Chapter 3 - Principles of Programming Languages
Chapter 3 - Principles of Programming Languages
Subprograms
Chapter 9 Topics
Introduction
Fundamentals of Subprograms
Design Issues for Subprograms
Local Referencing Environments
Parameter-Passing Methods
Parameters That Are Subprogram Names
Overloaded Subprograms
Generic Subprograms
Design Issues for Functions
User-Defined Overloaded Operators
Coroutines
Chapter 9
Subprograms
Introduction
Subprograms are the fundamental building blocks of programs and are therefore
among the most import concepts in programming language design.
The reuse results in several different kinds of savings, including memory space and
coding time.
Fundamentals of Subprograms
General Subprogram Characteristics
a. A subprogram has a single entry point.
b. The caller is suspended during execution of the called subprogram, which implies
that there is only one subprogram in execution at any given time.
c. Control always returns to the caller when the called subprograms execution
terminates
Basic Definitions
A subprogram definition is a description of the actions of the subprogram
abstraction.
A subprogram call is an explicit request that the called subprogram be executed.
A subprogram is said to be active if, after having been called, it has begun
execution but has not yet completed that execution.
The two fundamental types of the subprograms are:
o Procedures
o Functions
A subprogram header is the first line of the definition, serves several definitions:
o It specifies that the following syntactic unit is a subprogram definition of some
particular kind.
o The header provides a name for the subprogram.
o May optionally specify a list of parameters.
Consider the following header examples:
Fortran
Subroutine Adder(parameters)
Ada
procedure Adder(parameters)
void Adder(parameters)
No special word appears in the header of a C subprogram to specify its kind.
The parameter profile (sometimes called the signature) of a subprogram is the
number, order, and types of its formal parameters.
The protocol of a subprogram is its parameter profile plus, if it is a function, its return
type.
A subprogram declaration provides the protocol, but not the body, of the
subprogram.
A formal parameter is a dummy variable listed in the subprogram header and used
in the subprogram.
An actual parameter represents a value or address used in the subprogram call
statement.
Function declarations are common in C and C++ programs, where they are called
prototypes.
Java and C# do not need declarations of their methods, because there is no
requirement that methods be defined before they are called in those languages.
Parameters
Subprograms typically describe computations. There are two ways that a non-local
method program can gain access to the data that it is to process:
1. Through direct access to non-local variables.
The only way the computation can proceed on different data is to
assign new values to those non-local variables between calls to the
subprograms.
Extensive access to non-locals can reduce reliability.
2. Through parameter passing more flexible.
Data passed through parameters are accessed through names that are
local to the subprogram.
A subprogram with parameter access to the data it is to process is a
parameterized computation.
It can perform its computation on whatever data it receives through its
parameters.
A formal parameter is a dummy variable listed in the subprogram header and
used in the subprogram.
Subprograms call statements must include the name of the subprogram and a list of
parameters to be bound to the formal parameters of the subprogram.
An actual parameter represents a value or address used in the subprogram call
statement.
Actual/Formal Parameter Correspondence:
1. Positional: The first actual parameter is bound to the first formal parameter
and so forth. Practical if list is short.
2. Keyword: the name of the formal parameter is to be bound with the actual
parameter. Can appear in any order in the actual parameter list.
In C++, which has no keyword parameters, the rules for default parameters are
necessarily different.
The default parameters must appear last, for parameters are positionally associated.
Once a default parameter is omitted in a call, all remaining formal parameters must
have default values.
Ada subprograms and the methods of C++, Java, and C# have only stack-
dynamic local variables.
Parameter Passing Methods
Semantic Models of Parameter Passing
Formal parameters are characterized by one of three distinct semantic models:
o in mode: They can receive data from corresponding actual parameters.
o out mode: They can transmit data to the actual parameter.
o inout mode: They can do both.
There are two conceptual models of how data transfers take places in parameter
transmission:
o Either an actual value is copied (to the caller, to the callee, or both ways), or
o An access path is transmitted.
Most commonly, the access path is a simple pointer or reference.
Figure below illustrates the three semantics of parameter passing when values are
copied.
2. Pass-by-Result
Pass-by-Result is an implementation model for out-mode parameters.
When a parameter is passed by result, no value is transmitted to the subprogram.
The corresponding formal parameter acts as a local var, but just before control is
transferred back to the caller, its value is transmitted back to the callers actual
parameter, which must be a var.
One problem with the pass-by-result model is that there can be an actual parameter
collision, such as the one created with the call.
sub(p1, p1)
In sub, assuming that the two formal parameters have different names, the two can
obviously be assigned different values.
Then whichever of the two is copied to their corresponding actual parameter last
becomes the value of p1.
3. Pass-by-Value-Result
It is an implementation model for inout-mode parameters in which actual values are
copied.
It is a combination of pass-by-value and pass-by-result.
The value of the actual parameter is used to initialize the corresponding formal
parameter, which then acts as a local var.
At subprogram termination, the value of the formal parameter is transmitted back to
the actual parameter.
It is sometimes called pass-by-copy because the actual parameter is copied to the
formal parameter at subprogram entry and then copied back at subprogram
termination.
4. Pass-by-reference
Pass-by-reference is a second implementation model for inout-mode parameters.
Rather than copying data values back and forth. This method transmits an access
path, sometimes just an address, to the called subprogram. This provides the
access path to the cell storing the actual parameter.
The actual parameter is shared with the called subprogram.
Advantages:
The passing process is efficient in terms of time and space. Duplicate space is
not required, nor is any copying.
Disadvantages:
Access to the formal parameters will be slower than pass-by-value, because of
additional level of indirect addressing that is required.
Inadvertent and erroneous changes may be made to the actual parameter.
Aliases can be created as in C++.
fun(total, total)
Then first and second in fun will be aliases.
5. Pass-by-Name
The method is an inout-mode parameter transmission that doesnt correspond to a
single implementation model.
When parameters are passed by name, the actual parameter is, in effect, textually
substituted for the corresponding formal parameter in all its occurrences in the
subprogram.
A formal parameter is bound to an access method at the time of the subprogram
call, but the actual binding to a value or an address is delayed until the formal
parameter is assigned or referenced.
Because pass-by-name is not used in any widely used language, it is not discussed
further here
Java
o All parameters are passed are passed by value
o Object parameters are 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.
Ada
o Three semantics modes of parameter transmission: in, out, in out; in is the
default mode
o Formal parameters declared out can be assigned but not referenced; those
declared in can be referenced but not assigned; in out parameters can be
referenced and assigned
C#
o Default method: pass-by-value
o Pass-by-reference is specified by preceding both a formal parameter and its
actual parameter with ref
PHP: very similar to C#
Perl: all actual parameters are implicitly placed in a predefined array named @_
Type-Checking Parameters
It is now widely accepted that software reliability demands that the types of actual
parameters be checked for consistency with the types of the corresponding formal
parameters.
Ex:
result = sub1(1)
function sub1( ) {
var x;
function sub2( ) {
alert(x); // Creates a dialog box with the value of x
};
function sub3( ) {
var x;
x = 3;
sub4(sub2);
};
function sub4(subx ) {
var x;
x = 4;
subx( );
};
x = 1;
sub3( );
};