Concepts of Programming Languages

(Sean Pound) #1
9.5 Parameter-Passing Methods 407

void fun(const int &p1, int p2, int &p3) {... }

where p1 is pass-by-reference but cannot be changed in the func-
tion fun, p2 is pass-by-value, and p3 is pass-by-reference. Nei-
ther p1 nor p3 need be explicitly dereferenced in fun.
Constant parameters and in-mode parameters are not exactly
alike. Constant parameters clearly implement in mode. However,
in all of the common imperative languages except Ada, in-mode
parameters can be assigned in the subprogram even though those
changes are never reflected in the values of the corresponding
actual parameters. Constant parameters can never be assigned.
As with C and C++, all Java parameters are passed by value.
However, because objects can be accessed only through refer-
ence variables, object parameters are in effect passed by reference.
Although an object reference passed as a parameter cannot itself
be changed in the called subprogram, the referenced object can be
changed if a method is available to cause the change. Because ref-
erence variables cannot point to scalar variables directly and Java
does not have pointers, scalars cannot be passed by reference in
Java (although a reference to an object that contains a scalar can).
Ada and Fortran 95+ allow the programmer to specify in
mode, out mode, or inout mode on each formal parameter.
The default parameter-passing method of C# is pass-by-
value. Pass-by-reference can be specified by preceding both a for-
mal parameter and its corresponding actual parameter with ref.
For example, consider the following C# skeletal method and call:

void sumer(ref int oldSum, int newOne) {... }

sumer(ref sum, newValue);

The first parameter to sumer is passed by reference; the second is passed by
value.
C# also supports out-mode parameters, which are pass-by-reference
parameters that do not need initial values. Such parameters are specified in the
formal parameter list with the out modifier.
PHP’s parameter passing is similar to that of C#, except that either the
actual parameter or the formal parameter can specify pass-by-reference. Pass-
by-reference is specified by preceding one or both of the parameters with an
ampersand.
Perl employs a primitive means of passing parameters. All actual param-
eters are implicitly placed in a predefined array named @_ (of all things!). The
subprogram retrieves the actual parameter values (or addresses) from this array.
The most peculiar thing about this array is its magical nature, exposed by the
fact that its elements are in effect aliases for the actual parameters. There-
fore, if an element of @_ is changed in the called subprogram, that change is
reflected in the corresponding actual parameter in the call, assuming there is a

History Note


History Note


ALGOL 60 introduced the
pass-by-name method. It also
allows pass-by-value as an
option. Primarily because of
the difficulty in implementing
them, pass-by-name parameters
were not carried from ALGOL
60 to any subsequent languages
that became popular (other
than SIMULA 67).


history note


ALGOL W (Wirth and Hoare,
1966) introduced the pass-by-
value-result method of parameter
passing as an alternative to
the inefficiency of pass-by-
name and the problems of
pass-by-reference.

Free download pdf