Programming Fundamentals/Call by Value vs Call by Reference

Overview

edit

In call by value, a parameter acts within the function as a new local variable initialized to the value of the argument (a local (isolated) copy of the argument). In call by reference, the argument variable supplied by the caller can be affected by actions within the called function.[1]

 

Discussion

edit

Call by Value

edit

Within most current programming languages, parameters are passed by value by default, with the argument as a copy of the calling value. Arguments are isolated, and functions are free to make changes to parameter values without any risk of impact to the calling function. Consider the following pseudocode:

Function Main
    Declare Real fahrenheit

    Assign fahrenheit = 100
    Output "Main fahrenheit = " & fahrenheit
    Call ChangeFahrenheit(fahrenheit)
    Output "Main fahrenheit = " & fahrenheit
End

Function ChangeFahrenheit (Real fahrenheit)
    Output "ChangeFahrenheit fahrenheit = " & fahrenheit
    Assign fahrenheit = 0
    Output "ChangeFahrenheit fahrenheit = " & fahrenheit
End

Output

edit
Main fahrenheit = 100
ChangeFahrenheit fahrenheit = 100
ChangeFahrenheit fahrenheit = 0
Main fahrenheit = 100

In English, the Main function assigns the value 100 to the variable fahrenheit, displays that value, and then calls ChangeFahrenheit passing a copy of that value. The called function displays the argument, changes it, and displays it again. Execution returns to the calling function, and Main displays the value of the original variable. With call by value, the variable fahrenheit in the calling function and the parameter fahrenheit in the called function refer to different memory addresses, and the called function cannot change the value of the variable in the calling function.

Call by Reference

edit

If a programming language uses or supports call by reference, the variable in the calling function and the parameter in the called function refer to the same memory address, and the called function may change the value of the variable in the calling function. Using the same code example as above, call by reference output would change to:

Main fahrenheit = 100
ChangeFahrenheit fahrenheit = 100
ChangeFahrenheit fahrenheit = 0
Main fahrenheit = 0

Programming languages that support both call by value and call by reference use some type of key word or symbol to indicate which parameter passing method is being used.

Language Call By Value Call by Reference
C++ default use &parameter in called function
C# default use ref parameter in calling and called functions
Java default applies to arrays and objects
JavaScript default applies to arrays and objects
Python default applies to arrays (lists) and mutable objects

Arrays and objects are covered in later chapters.

Key Terms

edit
call by reference
Parameters passed by calling functions may be modified by called functions.
call by value
Parameters passed by calling functions cannot be modified by called functions.

References

edit