The nature of pass-value and pass-address calls
When the function receives any arguments, it makes a copy of the arguments themselves. The pass-value call is easy to understand. The function creates a new variable and assigns the value of the parameter variable to the new variable. The address pass call actually copies the value of the passed pointer argument (an int value, which means the location of a variable in memory. We call this int the memory address), creates a new pointer variable, and assigns the copied value to the new pointer variable. As you can see, the pass-value call and the pass-address call are highly consistent. Once you understand this, it’s easy to draw the following conclusions.
Application scenarios
Pass call:
- The function uses only the value of the parameter, and does not need to modify the value of the parameter.
Address call:
- The function modifies the value of the argument’s original variable
- The structure/block is large enough that copying the entire structure/block would be too time-consuming
Note: c++ also has the concept of reference, which is essentially a syntactic sugar for passing an address call, simplifying the process of explicitly identifying Pointers. In languages such as Java/Python, all incoming calls are pass-address calls, which are safe because direct manipulation Pointers are shielded.