First, the theoretical part
The function parameters in C + + are mainly divided into two categories, 1,
Figure 1
Summarize:
When a function argument is a non-reference parameter, a copy of the argument is passed into the body of the function (note that for the base type, the copy is the value of the argument, and the address of the actual parameter is copied for the pointer )
(1) If the formal parameter is a non-const basic type , you can receive the Const argument or receive the non-const argument. Just modifying the value of the parameter in the function body does not affect the value of the argument.
Because for a parameter of a primitive type, the value passed into the function body is a copy of the actual parameter, not the argument itself, so modifying the value of the argument in the body of the function does not affect the argument.
(2) If the parameter is a pointer type other than const, You can receive the Const argument, or you can receive non-const arguments. Modifying the value of a parameter in a function body affects The value of the argument.
Because for a parameter of a pointer type, passing near the body of a function is a copy of the address of the argument pointer, the parameter pointer and the argument pointer point to the same address, and the value that is pointed to by the modified argument pointer affects the value that the argument pointer points to.
(3) If the formal parameter is a const basic type, you can receive the Const argument, or you can receive non-const arguments. Only the values of the parameters cannot be modified in the function body.
(4) If the parameter is a const pointer type, you can only receive pointer arguments of the const type, and you cannot receive non-const pointer arguments.
Const pointer Initialization rule: You can point a pointer to a const object to a non-const object, and you can not point a pointer to a non-const object to a const object.
When a function is a reference parameter, the argument itself is passed in the body of the near function, because the reference is the alias of the variable, and changing the value of the reference affects the value of the variable.
(1) The reference parameter is the alias of the argument and can be used to modify the argument.
(2) The reference parameter can have an additional return value.
(3) For cases where the parameter value is not modified in the function body, the formal parameter should be set to const &, or const reference, which reduces the copy assignment of the value.
(4) If a reference to a pointer changes in the function body, the value of the object to which the pointer points is changed.
Second, the code snippet is as follows:
Code Snippet 1:
int add_1 (int x,int y) {return x+y;} int add_2 (const int X,const int y) { //x++;//error,const parameter cannot modify return x+y;} int main () {int a=1,b=2;const int m=9;const int n=8;add_1 (A, B)//okadd_1 (m,n);//okadd_2 (m,n);//okadd_2 (A, B);// Okreturn 0;}
The conclusion in fragment 1:
When the parameter type is a base data type, non-const parameters can receive const arguments, and they can receive non-const arguments, and when the parameter type is the base data type, the const parameter can receive the Const argument, and it can also receive non-const arguments. When a parameter is a const argument, the value of the parameter cannot be modified in the body of the function. Code Snippet 2:
void add_1 (int *a) {*a =
The conclusion in Fragment 2:
When a parameter is a const pointer, you cannot modify the value that the pointer points to. When a parameter is a non-const pointer, only non-const pointer arguments can be received and const pointer arguments cannot be received. When a parameter is a const pointer, you can either receive a non-const pointer argument or receive a const pointer argument. Code Snippet 3:
The reference parameter, which is the alias of the argument, modifies the value of the argument void add (int &x) {x = x +1;} The reference parameter is equivalent to the extra return value void fuc2 (int i,int j,int &jf,int &jf2,int &cf,int &cf2) {JF = i + j;jf2 = I-J;CF = i * j; CF2 = i/j;} Parameters that do not need to be modified in the body of the function should be set to const &, that is, a const reference//Point reference to the address void cund (int *&x,int *&y) {int *temp = Y;y = X;x = temp;} int Main () {int i = 9;cout<< "before" <<i<<endl;//9add (i);cout<< "after" <<i<<endl;// 10int j = 20;int v1,v2,v3,v4;fuc2 (i,j,v1,v2,v3,v4);cout<< "addition result:" <<v1<<endl;cout<< "subtraction Result:" <<v2<<endl;cout<< Multiplication Result: <<v3<<endl;cout<< Division Result: <<v4<<endl; cout<< "===========================" <<endl;int *ptri = &i;//ptri pointer points to iint *PTRJ = &j;//ptrj Pointer to J cout<< "before Exchange:" <<i << "," <<j<<endl; i = ten J = 20cout<< "before Exchange-pointers:" <<*ptri << "," <<*ptrj<<endl; *ptri = *PTRJ = 20cund (PTRI,PTRJ);//Exchange is the value of the pointer, not the value of I,j cout<<" After Exchange: "<<i <<", "<<j<<endl; i = ten J = 20cout<< "After Exchange-pointers:" <<*ptri << "," <<*ptrj<<endl; *ptri = *PTRJ = 20return 0;}
C + + Learning basic Five function parameter--formal parameter