Reference vs. Value
Actual parameter: the value of the parameter in the calling program. Formal parameter: the name of the parameter in the subroutine.
A call creates a binding between the formal parameter name in the subroutine and the actual parameter value in the call.
Call by reference and call by value have opposite advantages and disadvantages:
Call by: | Advantage | Disadvantage |
Reference | Fast for Arrays | Possibly Dangerous |
Value | Safe | Slow for Arrays |
Usually, we want to use call by value for small values such as numbers and call by reference for arrays.
Main: i = 3; foo(j): j = j + 1; foo(i); print j; print i; Results: Value: j = 4, i = 3 Reference: j = 4, i = 4