Concepts of Programming Languages

(Sean Pound) #1
9.2 Fundamentals of Subprograms 391

C++ programs, where they are called prototypes. Such declarations are often
placed in header files.
In most other languages (other than C and C++), subprograms do not need
declarations, because there is no requirement that subprograms be defined
before they are called.

9.2.3 Parameters


Subprograms typically describe computations. There are two ways that a non-
method subprogram can gain access to the data that it is to process: through
direct access to nonlocal variables (declared elsewhere but visible in the sub-
program) or through parameter passing. Data passed through parameters are
accessed through names that are local to the subprogram. Parameter passing is
more flexible than direct access to nonlocal variables. In essence, a subprogram
with parameter access to the data that it is to process is a parameterized com-
putation. It can perform its computation on whatever data it receives through
its parameters (presuming the types of the parameters are as expected by the
subprogram). If data access is through nonlocal variables, the only way the
computation can proceed on different data is to assign new values to those
nonlocal variables between calls to the subprogram. Extensive access to non-
locals can reduce reliability. Variables that are visible to the subprogram where
access is desired often end up also being visible where access to them is not
needed. This problem was discussed in Chapter 5.
Although methods also access external data through nonlocal references
and parameters, the primary data to be processed by a method is the object
through which the method is called. However, when a method does access
nonlocal data, the reliability problems are the same as with non-method sub-
programs. Also, in an object-oriented language, method access to class variables
(those associated with the class, rather than an object) is related to the concept
of nonlocal data and should be avoided whenever possible. In this case, as well
as the case of a C function accessing nonlocal data, the method can have the
side effect of changing something other than its parameters or local data. Such
changes complicate the semantics of the method and make it less reliable.
Pure functional programming languages, such as Haskell, do not have
mutable data, so functions written in them are unable to change memory in
any way—they simply perform calculations and return a resulting value (or
function, since functions are values).
In some situations, it is convenient to be able to transmit computations,
rather than data, as parameters to subprograms. In these cases, the name of
the subprogram that implements that computation may be used as a param-
eter. This form of parameter is discussed in Section 9.6. Data parameters are
discussed in Section 9.5.
The parameters in the subprogram header are called formal parameters.
They are sometimes thought of as dummy variables because they are not variables
in the usual sense: In most cases, they are bound to storage only when the subpro-
gram is called, and that binding is often through some other program variables.
Free download pdf