C++ forms participate in the difference of argument instance resolution

  • 2020-04-02 02:31:36
  • OfStack

This paper illustrates the differences between C++ form participation arguments with examples, which will help readers deepen their understanding of C++ form participation arguments.

The parameter appears in the function definition and can be used throughout the body of the function, but cannot be used without the function. Arguments appear in the main call function, after entering the called function, the real variables can not be used. The function of parameter and argument is data transfer. When a function is called, the argument value is passed to the parameter of the called function so as to transfer the argument value to the data of the called function.

1. The parameter only allocates the memory unit when it is called. At the end of the call, the allocated memory unit is immediately released. Therefore, the parameter is only valid within the function. After the function call returns the main function, the parameter can no longer be used.

Arguments can be constants, variables, expressions, functions, etc., no matter what type of quantity the arguments are, they must have certain values when the function is called in order to pass these values to the parameters. Therefore, the method of assignment, input and so on should be used in advance to obtain the definite value of arguments.

3. Arguments and parameters should be strictly consistent in number, type and order, otherwise "type mismatch" will occur.

4. The data transfer in the function call is one-way. That is, the value of the argument can only be passed to the parameter, and the value of the parameter cannot be passed to the parameter in reverse. So the value of the parameter changes during the function call, while the value of the argument does not.

Refer to the following example:


void Exchg1(int x, int y) 
{
 int tmp;
 tmp=x;
 x=y;
 y=tmp;
 printf("Exchg1:x=%d,y=%dn",x,y);
}
void Exchg2(int &x, int &y) 
{
 int tmp;
 tmp=x;
 x=y;
 y=tmp;
 printf("Exchg2:x=%d,y=%dn",x,y);
}
void Exchg3(int *x, int *y) 
{
 int tmp;
 tmp=*x;
 *x=*y;
 *y=tmp;
 printf("Exchg3:x=%d,y=%dn",*x,*y);
}

void main()
{
 int a=4,b=6;
 Exchg1 (a,b) ;
 printf("a=%d,b=%dn",a,b);
 Exchg2 (a,b);
 printf("a=%d,b=%dn",a,b);
 Exchg3(&a,&b) ;
 printf("a=%d,b=%dn",a,b);
}

When the Exchg1 function is called, the data of a and b is not exchanged successfully. Why?
Int a = 4 b = 6;
Exchg1 (a, b);     // what is essentially happening here is: Exchg1 (intx=a,int y=b);     X and y are the parameters in the definition of the function, which means that we just assign the values of the arguments a and b to the variables x and y. And then, the swap that happens in the function just replaces the values of x and y, and the arguments a and b don't matter.
Then look at Exchg2 (a, b);     // Exchg2 (int &x=a,int &y=b);     Here x and y are both references to a and b, so the operation x and y swap is the same thing as a and b swap, and of course, Exchg2 successfully swaps a and b
Exchg3 (& a & b);     / / Exchg3 (int * x = & a, int * y = & b);     X and y2 parameters are Pointers to a and b, which is the address where the real parameters are stored. And then you swap the values that x and y are pointing to, which is the arguments a and b, so the swap is also successful.

As a result of the code running, exchg1 does not exchange a and b values; Exchg2 exchanged a, b value, by exchg, a, b value seems to be no exchange, is still a is 4, 6 b, just began to think that there is something wrong with the code, then set the breakpoint, find code to run to exchg3 (& a, & b), a = 6, b = 4, and initial values are the same, so the code to run results show have exchanged a, b value, so that the code is without any problems.


Related articles: