next up previous contents
Next: Parameter Definition Syntax Up: Parameter Passing for Subroutines Previous: Parameter Passing for Subroutines   Contents

What is a parameter?

Imagine the definition of a subroutine as an office/cubicle with a worker who specializes in a particular task. A parameter is essentially an in-box and an out-box for the office/cubicle. Each parameter has a unique name so the worker in the office/cubicle can refer to it.

There are two types of parameters. One type of parameter is a copy of some original item, while the other type is a reference to an original item.

The first type of parameters is called ``passed by value''. When a parameter is passed by value, the invoker makes a copy of some value, then give the subroutine the copy to process. The main feature of this type of parameters is that the subroutine can modify the copy as much as possible during processing, but it can never modify the original item. This type of parameter is useful mostly for information that is given to a subroutine.

The second type of parameters is called ``passed by reference''. When a parameter is passed by reference, the invoker gives the subroutine a ``reference'' to an object (i.e., a variable). A ``reference'' is essentially some instruction to access the original item. When a parameter is passed by reference, the subroutine accesses the original item directly. As a result, any changes the subroutine makes to the parameter is reflected directly at the original item. This type of parameter is useful for passing information that a subroutine needs to modify and return to the invoker.


next up previous contents
Next: Parameter Definition Syntax Up: Parameter Passing for Subroutines Previous: Parameter Passing for Subroutines   Contents
Tak Auyeung 2003-12-03