Today I suddenly see a question about formal parameters and arguments, I actually superficial understanding. Despise the past in my mind only one parameter of the concept, the difference between formal parameters and arguments really do not know, as a study for a few years C + + people, really deeply feel sorry for the C + + teacher T. T
I think as long as you understand the value of the delivery and address delivery, you should be able to understand the formal parameters and actual parameters of the details of the work.
1. Value passing
An argument is a variable, and an expression is equivalent.
Find (int x) {}
y= find (z);
In the above example, Z is an argument and x is a formal parameter. X is changed to Z.
During value passing, the arguments and parameters are located in two different addresses in memory, and the arguments are copied once and copied to the parameters first. Therefore, the changes in the parameters do not have any effect on the arguments during value passing.
2. Address delivery (also referred to as reference passing)
The argument is a pointer.
In the case of a function call, the argument is passed to you as a pointer address, which means that the argument is the same as the formal parameter, and the argument changes when your parameter changes.
Find (int &x) {}
y= find (z);
In the above example, Z is an argument and x is a formal parameter. Z changes with X.
3. Const reference Delivery
Find (const int &x) {}
y= find (z);
In the above example, Z is an argument and x is a formal parameter. Z does not change with X.
Some people will ask, you do this is not the same as the value of the pass? No!
Careful observation will find that in the value of the transfer of two copies, waste of memory resources is quite shameful, the appearance of const effectively avoid the occurrence of this situation, just copy once is enough.
C Language Learning Notes (003)-arguments and formal parameters in C + + (GO)