Uploaded by alireza.innovates

call by

advertisement
PARAMETER PASSING
1990. Metcalf, M., and Reid, J. Fortran 90 Explained. New
York: Oxford University Press.
1994. Koelbel, C. H., Loveman, D. B., Schreiber, R. S., Steele,
G. L., Jr, and Zosel, M. E. The High Performance Fortran
Handbook. Cambridge, MA: MIT Press.
1996. Wolfe, M. J. High Performance Compilers for Parallel
Computing. Reading, MA: Addison-Wesley.
1999. Gropp W., Lusk, E., and Skjellum, A. UsingMPI: Portable
Parallel Programming with the Message-Passing Interface,
2nd Ed. Cambridge, MA: MIT Press.
Michael J. Quinn
(a) Call by value (Pascal). X holds the value of Y
operations on X cannot affect Y or Z .
*
1365
2 and
Calling
program
Procedure
p r o c e d u r e iPn(tXe g: e r ) ;
P (Y*Z)
(b) Call by reference (Pascal). Since W and X hold the locations
of Y and 2 , whatever P does to the variables W and X is
stored in Y and Z.
Calling
program
Procedure
p r oPc (evdaurrW
e ,X:integer);
P(Y,Z)
When asubprogram is called, informationmay be
passed to it by means of a parameter, or argument.
The placeholder within the subprogram for this information iscalled the formal parameter;the information
that is actually passed on a given invocation is called
the actual parameter. This article is concerned with
the means by which the actual parameters supplied to
a procedure or function are transferred to the formal
parameters. There are threebasic techniques:
( c ) Call by name (Algol). Within P, X will hold the value of Y
which is evaluated whenever the value of X is needed.
* Z,
Calling
program
Procedure
procedure P(X: i n t e g e r ) ;
P(Y.2)
i
i
thunk to compute
Y*Z in calling environment,
1. Call by value
2. Call by reference
3. Callby name
Figure 1.
Passing arguments to procedures.
Call by Value
In call by value, the actual parameter is evaluated at
the time of the subprogram call and its value is copied
into the formal parameter. There are two important
points:
1. The actual parameter is evaluated whether or not
the formal parameter is ever used inside the subprogram. For this reason call by value is sometimes
called eager evaluation.
2 . Since the formal parameter receives a copy of
the actual parameter, modifications to the formal
parameter have no effect on the actual parameter.
In this manner the calling routine is insulated from
the actions of the subprogram.
Callby
value is illustrated using Pascal syntaxin
Fig. la.
Call by Reference
In call by reference, the calling routine does not
provide the value of the actual parametertothe
subprogram but instead provides the address of the
memory location at which that value can be found.
Thus the formal parameter shares memory with the
actual parameter. It is the responsibility of the subprogram to access the data throughthis address; from
the programmer’s standpoint, the formal and actual
parameters sharememory directly. Call by reference is
illustrated using Pascal syntax in Fig. lb.
For an actual parameter that is an expression rather
than a variable name, like Y*z in Fig. la, there is no
automatically corresponding address in the calling
program. Therefore, if Y*z were passed by reference,
the calling routine would have to create a location for
the value of Y*z , evaluate Y* z , put it in this location,
and then transfer the address of this location to the
subprogram, having essentially the same effect as passing by value. Some languages, such as Fortran ( q . v . ) ,
do exactly this; most others, such as Pascal ( q . ~ . )do
,
not allow expressions to be passed by reference.
The difference between call by reference and call by
value is important in two ways:
1. In call by value, modification of a formal parameter
does not affectthe value of the corresponding actual
parameter. But in call by reference, the formal and
1366
PARAMETER PASSING
actual parameters share memory, so anymodification of a formal parameter modifies the actual
parameter as well. This means that in call by value
information can be passedonly into the subprogram, while in call byreference information can be
passed both into the subprogram and back to the
calling routine. For this reason value parameters are
sometimes called input parameters and reference
parameters are sometimes called outputparameters or input-output parameters.
2. In call by value, a copy of the entire actual parameter is passed to the subprogram, while in call by
reference a single address is passed. This means that
when a large data structure is passed, significant
space savings can berealized using callby reference
instead of call by value. In C++ ( q . v . ) ,the economy
of call by reference can be combined with
the safety
of call by value by declaring a by-reference input
parameter with the keyword
const, whichprevents its being changed by the subprogram.
parameter is referenced the closure is executed and
the current value of the argument expression is determined and used as the value of the parameter. Such
values may change during the execution of the subprogram as the result of side effects. When the actual
parameter is a simple (Le.unsubscripted) identifier the
process of callby
name isequivalent
to call by
reference, but interesting differences can arise when
an expression or asubscript and an array variable that
depends on that subscript are passed by name.
Call by name wasused in Algol ( q . ~ . but
) , has notbeen
common. It is a form of late binding (see BINDING),
which offers flexibilitybut can bedifficult to use. Some
functional programming ( 4 . v . ) languages usecall by
need, or lazy evaluation(note contrast to eager evaluation), a variation on call by name in which the expression that is passed is evaluated exactly once, the first
time its value is demanded in the subprogram. In the
absence of side effects that complicate call by name,
call by need is semanticallyequivalent to call by name,
but may be moreefficient to implementas it eliminates
the reevaluation of the actual parameter.
An alternative to callby reference iscallbyvalue/
result, in which the value of the actual parameter is
copied into the formal parameter as for call by value,
Parameter Passing in Common Languages
but when the subprogram terminates the value of the
formal is copied backinto the actual one. This provides
Callby valueis the mostwidelyused
parameteran input-output parameter withouthaving the formal
passing mechanism, but many languages offer alterand actual parameters share memory as in call byrefernatives. In Fortran, all parameters arepassed by reference. Callby reference and callby value/result are
ence. In Pascal and C++, the programmermay choose
semantically equivalent if the subprogram does not
between call by value and call by reference. In C, all
make any nonlocal references to the actual parameter
parametersare passed by value, but the programand if it completes normally rather than by raising an
mer can explicitly pass the address of any variable, so
exception. Indeed, Ada ( 4 . v . ) provides an I N OUT
the effect of call by reference can be achieved as well.
parameter that provides information flow both from
In Java, all parameters are passed by value, but when a
the actual to theformal and from the formal back to the
reference variable is passedthe reference is copied, not
actual, but does not specify whether it will be implethe object it refers to, so objects are effectively passed
mented as call by reference or call by valueh-esult.
by reference. In Algol,all parameters are passed by
name unless call by value is specified. Functional proCall by Name
gramming languages aredivided into lazy or nonstrict
languages, such as Haskell, which usecall by need,
In call by name, the entire actual parameter expresand strict languages, such as Scheme [a dialect of Lisp
sion is passedto the subprogram (so that it mightbetter
( q . ~ . ) which
],
use call by value.
have been called call by expression). What is passed is
not the symbolic string that defines the expression, but
a machine language subprogram created by the compiler, sometimes called a closure or thunk. The use of
this term is due to P. Z. Ingerman (1961) and refers to
the fact that in such a closure, the environment in
which an expression is to beevaluated has already been
“thought out”by the compiler, hence “thunk.”In addition to the code to be evaluated, the closure contains
the referencing environment for variables inthat code,
since it maybe different from the referencing environment (Le. the association of names withobjects) in the
called subprogram. This is illustrated in Fig. I C using
Algol syntax. In the subprogram, each time the formal
Whatever the language used, the programmer must
alwaysbe aware of how that language implements
parameter passing. Otherwise, programs may not
execute as planned.
Bibliography
1961. Ingerman, P. 2. “Thunks,” Comm. of the ACM, 4, 1.
1964. Randell, B., and Russell, L. J. Algol 60 Implementation.
New York: Academic Press.
1986. Peyton Jones, S. L. The Implementation of Functional
Pvogramming Languages. Upper Saddle River, NJ: Prentice
Hall.
1989. Reilly, E. D.,
and Federighi, F.D.Pascalgoritkms. Boston,
MA: Houghton-Mifflin.
PARTIAL DIFFERENTIAL EQUATIONS
1996. Dybvig, R. K. The Scheme Programming Language,
2nd Ed. Upper Saddle River, NJ: Prentice Hall.
1996. Gosling, J., Joy, B., and Steele, Jr, G. L. The Java
Language Specification. Reading, MA: Addison-Wesley.
1997. Stroustrup, B. The C++ Programming Language, 3rd Ed.
Reading, MA: Addison-Wesley.
1998. Ghezzi, C., and Jazayeri, M. Programming Language
Concepts, 3rd Ed. New York: John Wiley.
1999. Thompson, S. Haskell: The Craft of Functional
Programming, 2nd Ed. Reading, MA: Addison-Wesley.
Adrienne Bloss and J. A. N. Lee
1367
A major application of high-performance digital computers is to the numerical solution of problems involving partialdifferentialequations
(PDEs). These
problems arise in scientific applications involving simulation and modeling in areas such as weather forecasting, fluid and supersonic flow, nuclear diffusion studies
for reactor design, elasticity, and many others. These
and other science and engineering problems are some
of the primaryuses
of vector and parallel supercomputers ( q . ~ . ) .
An important class of partial differential equations is
linear equations of second order in two independent
variables. The most general such equation is
L [ u ] = Au,,
+ 2BuXy+ CU,, + Du, + Eu? + FU = G,
where A, B , C, D, E , F , and G are functions depending
on x and y . This equation is said to be elliptic, hyperbolic, or parabolic at a mesh point ( x , y ) according to
whether thediscriminant B 2 - AC is negative, positive,
or zero, respectively. Simple examples are the Laplace
equation uxx+ uyy= 0 (elliptic); the wave equation
u,, - uyy= 0 (hyperbolic); and the heat or diffusion
equation uxx- uy = 0 (parabolic).
Two important classes of problems involving partial
differential equations are initial-value problems and
boundary-value problems. For an initial-value problemin two variables, the desired function u(x,y ) is
required to satisfy the differential equation in an
unbounded region R and to satisfy certain auxiliary
conditions on the boundary 6R.Such conditions might
involve prescribing the values of U ( X , y ) on 6R or (asin
the Cauchyproblem) u and the normal derivative
duldn might be prescribed on SR. For a boundaryvalue problem, the region R is bounded, and one prescribes either u, d u l d n , or a linear combination of u
and d u l d n on 6R.For the Laplace equation, these conditions correspond to Dirichlet, Neumann, or mixed
conditions, respectively.
Before attempting to solve a problem involving a partial differential equation,
one
should determine
whether or not itis we12 posed. To bewell posed,
there should exist a unique solution that depends continuously on the boundary data.For linear equations of
second order, boundary-value
problems
involving
hyperbolic or parabolic equations are usually not well
posed, nor are initial-value problems involving elliptic
equations.
A basic tool in the solution of partial differential
equations is the method of finite differences. Here one
covers the region under consideration with a mesh
(Le. a grid), usually consisting of horizontal and vertical lines, and one seeks approximate values of the
solution at theintersection points, or nodes. The partial
derivatives appearing in the PDE are approximatedby
Download