Introduction
Each parameter is created anew on each call to the function. The value used to initialize a parameter is the corresponding argument passed in the call.
Nonreference Parameters
Parameters that are plain, nonreference types are initialized by copying the corresponding argument. When a parameter is initialized with a copy, the function has no access to the actual arguments of the call. It cannot change the arguments.
Example 1
// return the greatest common divisor int gcd(int v1, int v2) { while (v2) { int temp = v2; v2 = v1 % v2; v1 = temp; } return v1; }
Inside the body of the while, we change the values of both v1 and v2. However, these changes are made to the local parameters and are not reflected in the arguments used to call gcd. Thus, when we call
gcd(i, j)
the values i and j are unaffected by the assignments performed inside gcd.
Pointer Parameters
A parameter can be a pointer in which case the argument pointer is copied. As with any nonreference type parameter, changes made to the parameter are made to the local copy. If the function assigns a new pointer value to the parameter, the calling pointer value is unchanged.
Example 2
void reset(int *ip) { *ip = 0; // changes the value of the object to which ip points ip = 0; // changes only the local value of ip; the argument is unchanged }
After a call to reset, the argument is unchanged but the object to which the argument points will be 0:
int i = 42; int *p = &i; cout << "i: " << *p << '\n'; // prints i: 42 reset(p); // changes *p but not p cout << "i: " << *p << endl; // ok: prints i: 0
If we want to prevent changes to the value to which the pointer points, then the parameter should be defined as a pointer to const.





