Concepts of Programming Languages

(Sean Pound) #1
9.7 Calling Subprograms Indirectly 419

the environment in which the procedure appears as a parameter
has no natural connection to the passed subprogram.
Shallow binding is not appropriate for static-scoped lan-
guages with nested subprograms. For example, suppose the
procedure Sender passes the procedure Sent as a parameter
to the procedure Receiver. The problem is that Receiver
may not be in the static environment of Sent, thereby making it
very unnatural for Sent to have access to Receiver’s variables.
On the other hand, it is perfectly normal in such a language for
any subprogram, including one sent as a parameter, to have its
referencing environment determined by the lexical position of
its definition. It is therefore more logical for these languages to
use deep binding. Some dynamic-scoped languages use shallow
binding.

9.7 Calling Subprograms Indirectly


There are situations in which subprograms must be called indi-
rectly. These most often occur when the specific subprogram to
be called is not known until run time. The call to the subprogram is made
through a pointer or reference to the subprogram, which has been set dur-
ing execution before the call is made. The two most common applications of
indirect subprogram calls are for event handling in graphical user interfaces,
which are now part of nearly all Web applications, as well as many non-Web
applications, and for callbacks, in which a subprogram is called and instructed
to notify the caller when the called subprogram has completed its work. As
always, our interest is not in these specific kinds of programming, but rather
in programming language support for them.
The concept of calling subprograms indirectly is not a recently devel-
oped concept. C and C++ allow a program to define a pointer to a function,
through which the function can be called. In C++, pointers to functions are
typed according to the return type and parameter types of the function, so
that such a pointer can point only at functions with one particular protocol.
For example, the following declaration defines a pointer (pfun) that can point
to any function that takes a float and an int as parameters and returns a
float:

float (*pfun)(float, int);

Any function with the same protocol as this pointer can be used as the initial
value of this pointer or be assigned to the pointer in a program. In C and C++,
a function name without following parentheses, like an array name without
following brackets, is the address of the function (or array). So, both of the fol-
lowing are legal ways of giving an initial value or assigning a value to a pointer
to a function:

History Note


The original definition of Pascal
(Jensen and Wirth, 1974)
allowed subprograms to be
passed as parameters without
including their parameter type
information. If independent
compilation is possible (which it
was not in the original Pascal),
the compiler is not even allowed
to check for the correct number
of parameters. In the absence
of independent compilation,
checking for parameter
consistency is possible but is a
very complex task, and it usually
is not done.

Free download pdf