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

The Trade-off

Clearly, the parameter passing approach used constrains the functionality of the called method: When pass-by-value is used, the called method cannot modify the actual parameters; when pass-by-reference is used, the called method is able to modify the actual parameters. In addition, the two approaches 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 a large struct, 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 © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.