Value versus Reference parameters
Value:
- Passes a value to the function. This value is stored in a separate memory cell
for the duration.
- On leaving the function, the memory cell is returned to available space.
- Changes made to the variable in the function do not affect the value in the main program.
Reference:
- The address of the variable is passed to the function.
- Changes made to the variable in the function affect the value in the main program.
Passing by reference is faster and takes less space.
Why ever pass by value?
- To protect against inadvertent modification of a variable.
- To force the developer to understand what should be done with the variables.