next up previous contents index
Next: Overloading Up: More About C++ Previous: Basics of Classes   Contents   Index

References and Function Arguments

C and C++ are ``call by value'' languages. Actual variables are not passed to a function; instead a copy of the value is made, and the function operates only on the copy. Though this feature causes occasional inconvenience, it prevents a variable from having its value changed by a function.

A function may accept arbitrary data structures as arguments. If a data structure is complicated and a function is called frequently (thousands or millions of times), implicit copying becomes a source of inefficiency. References bypass this problem: If a variable is passed by reference, only a pointer need be copied.

A variable that is passed by reference may be altered by the calling function. This technique of updating variables is often touted as a feature in C++ texts; however, such trickery circumvents the data encapsulation of calling by value. Passing a variable by reference so that a function can modify the value of a variable is considered less than ideal programming practice.


next up previous contents index
Next: Overloading Up: More About C++ Previous: Basics of Classes   Contents   Index
Andrew D. Hwang 2004-09-04