This paper illustrates the difference between the form participation in C + +, and helps readers to deepen their understanding of C + + form participation in real parameters.
The formal parameter appears in the function definition and can be used throughout the function, leaving the function unused. The actual parameter appears in the keynote function, and the actual parametric can not be used after the function is transferred. The function of formal parameters and arguments is data transfer. When a function call is made, the calling function transmits the value of the argument to the parameter of the called function to realize the data transfer of the keynote function to the modulated function.
1. parametric allocates memory cells only when called, releasing the allocated memory cells at the end of the call. therefore, the formal parameters are valid only within the function. When a function call finishes returning the calling function, the parameter variable is no longer available.
2. arguments can be constants, variables, expressions, functions , and so on, regardless of the type of argument, they must have a definite value to pass the value to the formal parameter when the function call is made. Therefore, the actual parameters should be given a definite value by means of assignment, input and so on.
3. arguments and formal parameters should be strictly consistent on a quantity, type, or order, otherwise a "type mismatch" error will occur .
4. The data transfer occurring in the function call is one-way . That is, the value of the argument can only be passed to the formal parameter, and the value of the parameter cannot be transferred back to the argument. Therefore, during a function call, the value of the formal parameter changes, and the value in the argument does not change.
Refer to the following example:
void Exchg1 (int x, int y) {int tmp;
Tmp=x;
X=y;
y=tmp;
printf ("exchg1:x=%d,y=%d\n", x,y);
} void Exchg2 (int &x, int &y) {int tmp;
Tmp=x;
X=y;
y=tmp;
printf ("exchg2:x=%d,y=%d\n", x,y);
} void Exchg3 (int *x, int *y) {int tmp;
Tmp=*x;
*x=*y;
*y=tmp;
printf ("exchg3:x=%d,y=%d\n", *x,*y);
} void Main () {int a=4,b=6;
EXCHG1 (A,B);
printf ("a=%d,b=%d\n", a,b);
EXCHG2 (A,B);
printf ("a=%d,b=%d\n", a,b);
EXCHG3 (&A,&B);
printf ("a=%d,b=%d\n", a,b); }
When the EXCHG1 function is called, the data of A and B are not exchanged successfully. Why, then?
int a=4,b=6;
EXCHG1 (A,B); What happens here essentially is: EXCHG1 (Intx=a,int y=b); X and y are the parameters of the function definition, which means that the values of argument A and B are only given to the X,y 2 parameter variables. Next, the value interchange that occurred in the function changed only the value of x and Y, and argument A and B had no effect.
Look again Exchg2 (a,b); Then look at the essential Exchg2 (int &x=a,int &y=b); Here x and Y are all a,b references, the operation x and y Exchange equals A and B, naturally, the call EXCHG2 can successfully exchange A and B.
EXCHG3 (&A,&B); EXCHG3 (int *x=&a,int *y=&b); The x and Y2 parameters are pointers to a and B, which is the address where the real parameter is stored. The function then swaps the data that X and Y point to, which is argument A and B, so the exchange is successful.
The results of the code run, EXCHG1 did not exchange A,b value, EXCHG2 Exchange A,b value, to the Exchg,a,b value does not seem to exchange, still is a 4,b to 6, just started to think that the code has a problem, then set the breakpoint, the discovery code runs to Exchg3 (&a, &B), a=6,b=4, so the code runs the same as the initial value, indicating that the value of the a,b has been exchanged, so that the code does not have any problems.