Transferred from: http://www.prglab.com/cms/pages/c-tutorial/control-structures/functions-ii.php2.3 function II (functions II) parameters are passed by value and by address (arguments passed by value and by reference)
In all the functions we have seen so far, all the parameters passed to the function are passed by value ). That is to say, when we call a function with parameters, the value passed to the function is the value of the variable rather than the variable itself. For example, assume that we use the following code to call our first function addition:
Int x = 5, y = 3, Z;
Z = addition (x, y );
In this example, we call the addition functionXAndYTo it, that is5And3Instead of two variables:
In this way, when the FunctionAdditionIts variable when calledAAndBThe values are changed5And3But any changes made to the variable A or B in the addition function will not affect the values of the variable X and Y outside the variable, because the variableXAndYInstead of passing them to functions, they only pass their values.
However, in some cases, you may need to control variables other than a function in a function. To implement this operation, we must use the parameter passed by address (arguments passed by reference), just like the function in the example belowDuplicate:
// Passing parameters by reference # Include <iostream. h>Void duplicate (Int & A, Int & B, Int & C) { A * = 2; B * = 2; C * = 2; } Int main () { Int x = 1, y = 3, Z = 7; Duplicate (x, y, z ); Cout <"x =" <x <", y =" <Y <", Z =" <z; Return 0; } |
X = 2, y = 6, Z = 14 |
The first thing to note is thatDuplicateIn the declaration (Declaration), each variable type is followed by an address character ampersand sign (&). Its role is to specify that the variable is passed by address (by reference ), instead of passing by value as usual ).
When passing a variable by address (pass by reference), we are passing the variable itself, any modification made to the variable in the function will affect the variable passed outside the function.
In another wayA, B, cAnd the parameters used to call the function (X, YAndZ), So if we operate a in the function, the X value outside the function will also change. Similarly, any change to B will also affect y and C.Z>.
This is why the main programMainThree Variables inX, YAndZAfter the function duplicate is called, the results are printed and their values are doubled.
If the following function is declared:
void duplicate (int& a, int& b, int& c)
We declare as follows:
void duplicate (int a, int b, int c)
That is, do not write an address character.Ampersand (&)The parameter addresses are not passed to the function, but their values are passed. Therefore, the output results displayed on the screenX, Y, ZWill not change, still1, 3, 7.
This method uses ampersand (&) to declare parameters passed by address "by reference" is only applicable in C ++. In C, we must use a pointer (pointers) for the same operation.
Passing by reference is an effective method for the function to return multiple values. For example, the following is a function that returns the first and last values of the first input parameter.
// More than one Returning Value # Include <iostream. h> Void prevnext (int x, Int & Prev, Int & next) { Prev = X-1; Next = x + 1; }Int main () { Int x = 100, Y, Z; Prevnext (x, y, z ); Cout <"previous =" <Y <", next =" <z; Return 0; } |
Previous = 99,next = 101 |