Most programming languages uses the passing of the parameters to the functions. The parameter can be a variable or the array. In passing the parameters, one should know what the resultant output will be and when to use the corresponding method.
There are basically two types of parameter passing:
1. Pass by value
2. pass by reference
3. pass by const-reference
1. Pass by Value
------------------------------------
When a parameter is passed by value, a copy of the parameter is made. Therefore, changes made to the passed parameter by the called function have no effect on the corresponding actual parameter. Eg.
void fl(int n) {
n++;
}
int main() {
int x = 2;
fl(x);
cout << x;
return 0;
}
In this example, fl's parameter is passed by value. Therefore, although f increments its formal parameter n, that has no effect on the actual parameter x. The value output by the program is 2 (not 3).
2. Reference Parameters
------------------------------------------
When a parameter is passed by reference, the actual parameter itself is passed (and just given a new name -- the name of the corresponding formal parameter). Therefore, any changes made to the formal parameter affect the actual parameter.
Eg.
void fl(int &n) {
n++;
}
int main() {
int x = 2;
fl(x);
cout << x;
}
In this example, fl's parameter is passed by reference. Therefore, n in fl is actually changing variable x, so the output of this program is 3.
If the function whose purpose is to compute two or more values,we can use reference parameters.
For example, if you want to read a list of integers from a file, and you want to know both how many integers were read, as well as the average value that was read, you might use a function like the following:
void f(istream &input, int &numRead, double &average) {
int k, sum = 0;
numRead = 0;
while (intput >> k) {
numRead++;
sum += k;
}
average = (double)sum/numRead;
}
Another common use of reference parameters is for a function that swaps two values:
void swap( int &j, int &k ) {
int tmp = j;
j = k;
j = tmp;
}
This is useful, for example, in sorting an array, when it is often necessary to swap two array elements. The following code swaps the jth and kth elements of array A:
swap(A[j], A[k]);
3. Const-Reference Parameters
---------------------------------------------------
Another reason to use reference parameters is when you don't want the function to modify an actual parameter, but the actual parameter is very large, and you want to avoid the overhead of creating a copy. Of course, this only works if the function does not modify its formal parameter. To be sure that the actual parameter is not "accidentally" modified, you should use a const-reference parameter. Declaring the parameter to be const tells the compiler that it should not be changed.
Eg.
void f(const IntList &L) {
-- the code here cannot modify L --
}
Because L is a const-reference parameter, it is the compiler's job to be sure that L is not modified by fl (and that means that no data members of L are modified). The compiler doesn't know how the Print function is implemented; it only knows how it was declared, so if it is not declared const, it assumes the worst, and complains that function fl modifies its const-reference parameter L.
4. Array Parameters
------------------------------------------------------
Another unfortunate thing about C++ arrays is that they are always passed by reference (even though you don't declare them to be reference parameters). For example:
void fl(int A[]) {
A[0] = 5;
}
int main() {
int B[10];
B[0] = 2;
fl(B);
cout << B[0] << endl; // the output is 5
}
Although fl's parameter looks like it is passed by value (there is no &), since it is an array it is actually passed by reference, so the assignment to A[0] is really assigning to B[0], and the program prints 5 (not 2).