Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

The Trade-off

Clearly, the parameter passing method used constrains the functionality of the called function: When pass-by-value is used, the called function cannot modify the actual parameters; when pass-by-reference is used, the called function is able to modify the actual parameters. In addition, the two methods have different time and space requirements that need to be understood in order to make the proper selection.

Pass-by-value creates a local variable and initializes that local variable by copying the value of the actual parameter. This means that space is used (on the stack) for the local variable and that time is taken to initialize that local variable. For small variables these penalties are insignificant. However, if the variable is large, the time and space penalties may become prohibitive.

On the other hand, pass-by-reference does not create a local variable nor does it require the copying of the actual parameter. However, because of the way that it must be implemented, every time a reference formal parameter is used to access the corresponding actual parameter, a small amount of extra time is taken to dereference the reference. As a result, it is typically more efficient to pass small variables by value and large variables by reference.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.