In the example above two functions are defined. Both increments the value of i by one, but fcn1 uses call-by-value as parameter passing mechanism and fcn2 uses call-by-reference. call-by-value means that a local copy of the variable is created in fcn1, the local copy is incremented by one. It will not affect the actual parameter in the calling program (nr print). call-by-value is used for input of values to the function. call-by-reference means that the variable i in fcn2 refers to the same variable as the actual parameter in the calling program, (nr print), so that when i is incremented in fcn2 it will also affect the value of the actual parameter. Call-by-reference is used when the parameter should transfer a value from the function (output parameter). Note also that When no return value is used, the function return type can be declared void. The underscore character canbeusedinvariablenames. InC ++ it has the same status as a letter. Example from running the program is na20.nada.kth.se 883)./a.out 0 1 2 2 #include <iostream.h> #include <math.h> Example 7 double scprod( int n, double x[], double y[] ); double x[10], y[10]; for( int i = 0 ; i < 10 ; i++ ) x[i] = i; y[i] = sin(x[i]); 8
cout << "Scalar product is " << scprod( 10, x, y ) << endl; double scprod( int n, double x[], double y[] ) double sum = 0; for( int i=0 ; i < n ; i++ ) sum += x[i]*y[i]; return sum; Above is an example of using arrays in C++. The function scprod computes the scalar product of the two vector parameters. The size of the array (10) in this case, must be given as a fixed number in the main program. The compiler reserves memory for the vector, so the size must be known when the program is compiled. A vector is declared with 10 elements, the elements are x[0], x[1],...,x[9]. Indexes always start from 0 in Cand C++. In a function, it is possible to leave out the size of a vector parameter. Arrays are passed as call-by-reference, so no memory needs to be allocated for the function parameters during the compilation. #include <iostream.h> Example 8 double scprod( int n, double x[], double y[] ); double *x, *y; cout << "Give vector size "; int n; cin >> n; x = new double[n]; y = new double[n]; for( int i = 0 ; i < n ; i++ ) x[i] = i; y[i] = sin(x[i]); cout << "Scalar product is " << scprod( n, x, y ) << endl; delete[] x; delete[] y; 9
This example shows the same thing as Example 7, but using dynamically allocated memory instead. Dynamic memory should be used when the size of the array can not be known when the program is compiled. Note that: The new statement allocates memory, in this case a double vector of n elements. Do not use new as a name for variables. The function scprod is used unchanged from Example 7. The memory is removed by delete[]. It is important to remember to delete memory which is no longer needed. If this is forgotten, for example inside a function which is called often, a so called memory leak has appeared. After many calls to the function, the memory will be used up and the program terminates with an error. All memory is deleted automatically when main finishes, so the delete statements in the code above are not really necessary. There are programming languages, for example Java, which can delete unused memory automatically, but in C++ we have to do it ourselves. x and y are declared as pointers (*x, *y). A vector in C++ is understood as a pointer to a sequence of variables. Element x[i] can equally well be refered to as *(x+i), which means what x plus i locations further points to. #include <iostream> using namespace std; class point private: int x; int y; public: point( int xi, int yi ); void move( int dx, int dy ); void wri(); ; point::point( int xi, int yi ) x = xi; y = yi; Example 9 10
void point::move( int dx, int dy ) x += dx; y += dy; void point::wri() cout << "(x,y) = " << x << "," << y << endl; point a(5,2); a.move(-2,4); point b(1,-1); b.wri(); a=b; This example shows the use of a class. A class is a datatype consisting of several variables and functions. The data and functions can be declared as private or public. The private data and functions can only be used by functions inside the class. The public data can be accessed from the outside. private is the default, if nothing else is written. Functions are declared inside the class. They can be defined there as well. In the example above the functions are instead defined outside of the class. In this case it is necessary to add classname:: to the function name, in order to distinguish between functions of the same name but which belong to different classes. The class is only a description of the data type, and does not allocate place in memory. It is the objects a and b in the main program that are the variables in memory, of the type described by the class declaration. The special function which has the same name as the class, and is without return type, is called constructor and is called automatically when an object is created. The purpose of the constructor is to ensure that all variables are initialized in a correct way. 11
The main program can only access the public part of the objects. This is done by the dot-notation (a.wri()). One of the ideas of object orientation is to hide the internal representation of the class from the main program. It is possible for example, to replace the variables x and y by a vector x[2] inside the class. The functions in the class have then to be rewritten, but the main program does not need to be changed. Thus all changes are kept as local as possible. This is of importance when the program is very big. Example from running the program: na20.nada.kth.se 962)./a.out (x,y) = 5,2 (x,y) = 3,6 (x,y) = 1,-1 (x,y) = 1,-1 12