1. function parameter passing method: Value passing
(1) An error message about value passing
Let's take a look at the following question:
In void exchg1 (int x, int y)/* definition, the X and Y variables are called the form parameters of the exchg1 function */
{
Int TMP;
TMP = X;
X = y;
Y = TMP;
Printf ("x = % d, y = % d. \ n", x, y );
}
Q: What do you think this function is doing?
A: Does it seem to be a logarithm of the values of X and Y?
For more information, see the following procedure:
Main ()
{
Int A = 4, B = 6;
Exchg1 (a, B);/* a, B is the actual parameter of the exchg1 function. */
Printf ("A = % d, B = % d. \ n", a, B );
Return (0 );
}
Q: What will the printf ("x = % d, y = % d. \ n", x, y) In exchg1 () output?
I will ask again: printf ("A = % d, B = % d. \ n", a, B) after exchg1 (); what is the output of the statement?
The output result of the program is:
X = 6, y = 4.
A = 4, B = 6.
Why not a = 6, B = 4? Strange: clearly, I substituted a and B into X and Y respectively, and completed the exchange of two variable values in the function, why are the values of A and B still not exchanged (still a = 4, B = 6, rather than a = 6, B = 4 )?If you have this question, it is because you do not know the relationship between real parameters A and B and the form parameters X and Y.
(2) A common sense of preparation
To illustrate this problem, I will first provide a code:
Int A = 4;
Int X;
X =;
X = x + 3;
No. Now I ask you: what is the final a value and what is the X value?
In this Code, you need to understand one thing:Although the value is assigned to X, the variable is not an X variable. Any modification to X will not change the variable. Haha! Although simple and take it for granted, it is a very important thing to know.
(3) Understanding the form of value transfer
Check the code for calling the exclusive function:
Main ()
{
Int A = 4, B = 6;
Exchg1 (A, B)/* The exchg1 function is called here */
Printf ("A = % d, B = % d. \ n", a, B );
}
The operation code completed when exchg1 (A, B) is as follows.
Intx = A;/* optional */
Inty = B;/* Attention: here, the first two rows are implicit operations when calling the function.*/
Int TMP;
TMP = X;
X = y;
Y = TMP;
Note that the first two sentences are added to the call to execute the exchg1 function:
Int x =;
Int y = B;
This is two implicit actions when a function is called. It does exist. Now I just explicitly write it out. The problem becomes clearer. (Now, what do you think is the switching operation of A and B variables in the function, or only the X and Y variables ?)
Originally, in fact, the function implicitly assigned values of real parameters A and B to X and Y respectively, then, no operations are performed on A and B in the body of the exchg1 function you wrote.Only the X and Y variables are exchanged. It Is Not A or B. Of course, the values of A and B have not changed! The function only transfers the values of A and B to X and Y by assigning values. All the operations in the function are the values of X and Y, not the values of A and B.This is the so-called parameter value transfer.
2. function parameter transfer method 2: Address Transfer
First look at a piece of code:
Void exchg2 (int * PX, int * Py)
{
Int TMP = * PX;
* PX = * py;
* Py = TMP;
Printf ("* PX = % d, * py = % d. \ n", * PX, * Py );
}
Main ()
{
Int A = 4;
Int B = 6;
Exchg2 (& A, & B );
Printf ("A = % d, B = % d. \ n", a, B );
Return (0 );
}
The output result is:
* PX = 6, * py = 4.
A = 6, B = 4.
Function interface: exchg2 (int * PX, int * Py ),Note: The PX and Py parameters are pointers.
Let's look at the call: exchg2 (& A, & B );
It substitutes the address of a (& A) into PX, and the address of B (& B) into py. Like the above value transfer, two implicit operations are performed during the function call: The & A, & B values are assigned to PX and Py.
Px = &;
Py = & B;
Haha! We found that it is actually no different from the value transfer,However, here we pass the address values of A and B to PX and Py,Instead of passing the content of A and B, the call of the entire exchg2 function is as follows:
Px = & A;/* cursor */
Py = & B;/* specifyNote that these two rows are implicit actions that call exchg2.*/
Int TMP = * PX;
* PX = * py;
* Py = TMP;
Printf ("* PX = % d, * py = % d. \ n", * PX, * Py );
In this way, there is an implicit value assignment operation for the first two rows. We can now see that,The pointer PX and Py values are the address values of the and B variables respectively. Next, the operations on * PX and * Py are of course the operations on the and B variables themselves.Therefore, the exchange in the function is the exchange of A and B values. This is the so-called address transfer (the addresses of A and B are transferred to PX and Py ), do you understand now?
3. function parameter transfer method 3: Reference Transfer
See the following code:
Void exchg3 (Int & X, Int & Y )/*Note that the format of the formal parameter at the definition is different from the value transfer.*/
{
Int TMP = x; X = y;
Y = TMP;
Printf ("x = % d, y = % d. \ n", x, y );
}
Main ()
{
Int A = 4;
Int B = 6;
Exchg3 (a, B );/*Note: The call method is the same as the value transfer method.*/
Printf ("A = % d, B = % d. \ n", a, B );
}
Output result:
X = 6, y = 4.
A = 6, B = 4./* the output result is different from the value passing. */
No. The code format is different from the value transfer, that is, in the definition:
Exchg3 (Int & X, Int & Y)
However, we found that the values of A and B were reversed. This shows that the variables A and B are modified in exchg3 (a, B), not just X and Y.
Let's first look at exchg3 (Int & X, Int & Y) in the definition of the exchg3 function ). The X and Y parameters are int variables. We can call a function (for example, exchg1 (a, B);) Like a value (for example, exchg3 (, B );).However, there is an ampersand "&" in front of X and Y. With this, when calling exchg3, the function will replace A and B with X and Y respectively,X and Y reference the and B variables respectively.In this way, the real parameters A and B are actually operated in the function,That is to say, the values of A and B can be directly modified in the function.
4. Compare value transfer with reference transfer: 1) The Function Definition Format is different:
The value is defined as exchg1 (int x, int Y );
The reference is passed here: exchg3 (Int & X, Int & Y );
2) the same format is used for calling:
Value Transfer: exchg1 (A, B );
Reference transfer: exchg3 (A, B );
3) functions are different:
In the value passing function, the operation is not the and B variables, but the values A and B are assigned to X and Y. The function operates only the X and Y variables instead of the and B variables, and the values of A and B are not modified by the exchg1 function.
In the reference transfer exchg3 (a, B) function, X and Y are replaced by A and B respectively. The function operates on the and B variables, so the values of A and B can be modified in the function.
5. Comparison between value transfer and address transfer:
Pass by value: the pass by value is unidirectional, and the form parameter is a copy of the real parameter. Changing the value of the form parameter does not affect the value of the external real parameter.
If the parameter is a constant or expression in the call process, or it is clearly indicated that it is passed by value during the definition process, that is, during the call process, the actual parameter value is copied and passed to the form parameter, and there is no connection between the form parameter and the actual parameter, any changes made to the form parameters during the process will not affect the actual parameters.
Pass by address: Address Transmission is bidirectional;
If the real parameter is a variable during the call process, or it is explicitly indicated that it is passed by address. Address-based transmission is a combination of real parameters and form parameters. During the call process, the actual parameter address is passed to the form parameter, that is, both the form parameter and the real parameter are in memory units with one address. Therefore, any changes made to the parameters during the call process will affect the real parameters. That is, the real parameter changes with the change of the form parameter.
6. Adaptive situations of pointer transfer and reference transfer:
Pointer passing and reference passing are generally applicable:
(1) Modify parameters in the function and modify the parameters to affect the caller.. Comparison value transfer, pointer/reference transfer can change fromInput parameters(In fact, it is directly modified on the memory of the real parameter. It is not changed when the value is transferred to copy the value of the real parameter to another memory address ).
(2) Another usage of pointer/reference transfer is: When a function actually needs to return multiple values, but only one value can be explicitly returned, another variable to be returned can be passed to the function as a pointer/reference, in this way, after the function is modified and returned, the caller can get the modified variable, which is equivalent toImplicit return value transferRight.
Detailed description of function parameter transfer methods