There are some considerations about how a function caller passes parameters to the callee. In general, function parameter transfer is divided into three situations: value, pointer and reference.
First, understand the concept of actual participation in formal parameters.
int func(int x)//x is a formal parameter
{
return x*x;
}
int main(void)
{
int a = 10;
func(a);//a is a real parameter
return 0;
}
In the above code, x is a formal parameter and a is a real parameter. The formal parameter x is a copy of the actual parameter a. &a and &x are completely different.
1. Pass the value
The so-called pass value, as the name implies, is to pass the value of the real parameter directly to the function. Because formal parameters are copies of actual parameters, the transmitted value cannot be changed. In C++, if the object is passed, then during the value transfer process, the copy constructor of the object will be called implicitly, which has a certain calculation execution overhead (equivalent to creating a temporary object, and the destructor of the temporary object is executed after the function call is completed).
void func(int x)//func adopts the form of passing values
{
x = x+1;
printf("x=%d\n", x);
}
int main(void)
{
int a = 0;
func(a);//The value of pass cannot be modified
printf("a=%d\n", a);
return 0;
}
Analysis: The above program adopts the parameter passing form of passing values, passing the value 0 of a to the func function. Since x is a copy of a (&a and &x are completely different), the value x=x+1 modifies the value of x and does not modify the value of a. Therefore, the result of the above program execution is:
x=1
a=0
2. Pass the pointer
Passing a pointer is to pass the address of the real parameter to the function. After returning, the content of the address changes, but the address itself cannot be modified, or the pointer points to another location. Passing a pointer can modify the value of the real parameter, and there will be no problem in C++ calling the copy constructor of the object. The efficiency of passing a pointer is higher than that of passing a value. Therefore, if you need to modify the value of the actual parameter, you cannot pass the value, but need to pass the pointer, etc.
However, passing a pointer is more complicated than passing a value. Once the pointer calculation moves out of the normal range, it will cause illegal access to the program, etc.
void func(int *x)//func adopts the form of passing pointers
{
*x = *x+1;
printf("*x=%d,x=%p,&x=%p\n", *x,x,&x);
}
int main(void)
{
int a = 0;
func(&a);//Pass the address of the actual parameter a to the function func
printf("a=%d,&a=%p\n", a,&a);
return 0;
}
Analysis: Passing a pointer can modify the value of the actual parameter, but cannot modify the value of the pointer itself (modification is invalid). According to the definition of pointer, *x is a, so *x=*x+1, that is, a = a+1, so the output result of the above code is:
*x=1
a=1
Passing a pointer is actually a kind of value passing, but this value is an address. That is to say, if the address of variable a is passed in the calling function func(), in the function func(), a pointer variable x is actually temporarily applied. The value of x is 0x1234, which is equivalent to assigning the address of a to x, but the address of x itself has no task relationship with a. In the function, the content stored in address 0x1234 can be modified, for example, it was 0, and it can be modified to 1, but You cannot change the address 0x1234, that is, for example, if you want x=0x2345, this is not wrong in the function func(), but after modification, the value of x is 0x2345, which means it points to another address, and the content of the position 0x2345 is also modified, which has nothing to do with 0x1234. After the function returns, because a's address is still 0x1234, because the modified in func() is 0x2345, it has nothing to do with a.
3. Passing quotation--C language does not support it
The so-called reference is actually an alias for a variable. Passing a reference is a parameter passing method introduced in C++. Passing a reference is actually a pointer to the passed actual argument, so the value of the actual argument can be modified. However, the citation feature tells us that once the citation is initialized, the citation cannot be changed. Therefore, passing references is actually convenient and simple to have the value transmission, and it also has the efficiency of passing pointers, and it does not have the danger of passing pointers, which is relatively safe.
void func(int &x)//func adopts the form of passing references
{
x = x+1;
printf("x=%d\n", x);
}
int main(void)
{
int a = 0;
func(a);//Pass the reference to the actual parameter a to the function func
printf("a=%d\n", a);
return 0;
}
Analysis: func is defined by passing reference. After the actual parameter a is passed to the function func, func can modify the value of the actual parameter. So the above program execution result is:
x=1
a=1
In short: the value passing cannot be modified, and if it is an object, it is less efficient; the pointer passing the actual parameter can be modified, which is more efficient, but it is prone to errors; the reference passing can be modified, which is more efficient, and it is not prone to errors.