Basic Definitions cont.• A subprogram declaration provides the protocol, but not the body, of the subprogram – Function declarations in C/C++ are often called prototypes – Subprogram de
Trang 1Chapter 9
Subprograms
Trang 2Chapter 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
Trang 4Fundamentals of Subprograms
• Each subprogram has a single entry point
• The calling (sub)program (caller) is suspended during execution of the called subprogram
(callee)
• Control always returns to the caller when the
callee’s execution terminates
Trang 5Basic Definitions
• A subprogram definition describes the interface
and the actions of the subprogram
• A subprogram header is the first part of the
definition, including the name, the kind of
subprogram, and the formal parameters
• The parameter profile (signature) of a subprogram
is the number, order, and types of its parameters
• The protocol is a subprogram’s parameter profile
Trang 6Basic Definitions (cont.)
• A subprogram declaration provides the protocol, but not the body, of the subprogram
– Function declarations in C/C++ are often called
prototypes
– Subprogram declarations in Ada/Pascal are
sometimes called forward declarations
• A formal parameter is a dummy variable listed in the subprogram header and used in the
subprogram
• An actual parameter represents a R-value or
L-value used in the subprogram call statement
Trang 7• There are two ways that a subprogram can
access to the data
– Through direct access to nonlocal variables
– Through parameter passing
• Parameter passing is more flexible
– Parameter passing is a parameterized computation
– Nonlocals that are visible to the subprogram where
access to them is desired or not poor reliability
Trang 8Actual/Formal Parameter
• Positional parameters
– The binding of actual parameters to formal
parameters is by position: the first actual
parameter is bound to the first formal parameter and so forth
– A good method for relatively short parameter lists
• Keyword parameters
– The name of the formal parameter to which an
actual parameter is to be bound is specified with the actual parameter
– Parameters can appear in any order
Trang 9Actual/Formal Parameter (cont.)
• Example: Ada
SUMER(LENGTH => MY_LENGTH, LIST => MY_ARRAY, SUM
=> MY_SUM);
where LENGTH, LIST and SUM are formal parameter
• Example: Ada and FORTRAN 90 allow to mix
positional and keyword parameters in a call
SUMER(MY_LENGTH, LIST => MY_ARRAY, SUM => MY_SUM);
Trang 10Formal Parameter Default Values
• In certain languages (e.g., C++, Ada), formal
parameters can have default values
• In C++, default parameters must appear last
because parameters are positionally associated
Trang 11Procedures and Functions
• There are two categories of subprograms
– Procedures are collection of statements that define parameterized computations
Procedures provide user-defined statements
– Functions structurally resemble procedures but are semantically modeled on mathematical functions
Functions provide user-defined operators
They are expected to produce no side effects
Trang 12Design Issues for Subprograms
• What parameter passing methods are provided?
• Are parameter types checked?
• Are local variables static or dynamic?
• Can subprogram definitions appear in other
subprogram definitions?
• What is the referencing environment of a passed subprogram?
• Can subprograms be overloaded?
• Can subprogram be generic?
• Can subprogram be compiled independently?
Trang 13Local Referencing Environments
• Local variables can be stack-dynamic
– Advantages
Support for recursion
Storage for locals is shared among subprograms
– Disadvantages
Allocation/deallocation, initialization time
Indirect addressing
Subprograms cannot be history sensitive
• Local variables can be static
– More efficient (direct addressing)
– No run-time overhead
Trang 14Local Referencing Environments (cont.)
• Language Examples:
FORTRAN 77 and 90: most are static, but the
implementor can choose either (user can force
static with SAVE)
C/C++: both (variables declared to be static,
default they are stack-dynamic)
ALGOL 60, Pascal, Java, and Ada: stack-dynamic only
Trang 15Parameter Passing Methods
– inout mode : Formal parameters can do both
• Conceptual models of transfer
– Physically move a value
Trang 16Models of Parameter Passing (cont.)
Trang 17Parameter Passing Methods (cont.)
• Ways in which parameters are transmitted to
and/or from callee
Trang 18Pass-by-Value (in-mode Model)
• The value of the actual parameter is used to
initialize the corresponding formal parameter
– Normally implemented by copying
Additional storage is required
Storage and copy operations can be costly
– Can be implemented by transmitting an access
path but not recommended
Must write-protect in the called subprogram
Accesses cost more (indirect addressing)
Trang 19Pass-by-Result (out-mode Model)
• When a parameter is passed by result
– No value is transmitted to the callee
– The corresponding formal parameter acts as a
local variable; its value is transmitted to caller’s actual parameter when control is returned to the caller
– Require extra storage location and copy
operation
Trang 20Potential Problems
• There can be an actual parameter collision
procedure sub(y: int, z: int); … sub(x, x); …
Value of x depends on order of assignments at the return
• The implementor may be able to choose between two different times to evaluate the addresses of the actual parameters: at the time of the call or at the time of the return
– Suppose that list[idx] is an actual parameter If idx
is changed by the callee, then the address of
list[idx] will change between the call and the return
Trang 21Pass-by-Value-Result (in-out Mode)
• A combination of pass-by-value and
pass-by-result
• Sometimes called pass-by-copy
• Formal parameters have local storage
• Disadvantages:
– Those of pass-by-result
– Those of pass-by-value
Trang 22Pass-by-Reference (in-out Mode)
• Pass an access path to the callee
• Also called pass-by-sharing
• Passing process is efficient (no copying and no duplicated storage)
• Disadvantages
– Slower accesses (compared to pass-by-value) to formal parameters
– Potentials for unwanted side effects
– Unwanted aliases (access broadened)
Trang 23Problems of Aliases
• Сollisions between actual parameters
void fun(int * first, int * second); …
fun(&total, &total);
fun(&list[i], &list[j]); // i = j
• Сollisions between formal parameters and
nonlocal variables that are visible
int * global; …
void sub(int * local) {
*global = …; *local = …; …
aliases
Trang 24Pass-by-Name (in-out Mode)
• By textual substitution
• Formal parameters are bound to an access
method at the time of the call, but actual
binding to a value takes place at the time of a
reference or assignment
• Allows flexibility in late binding
Trang 25Pass-by-Name (cont.)
• The form of the actual parameter dictates the
implementation model of pass-by-name
parameters If actual parameter is
– A scalar variable, by-name is equivalent to by-reference
pass-– A constant expression, pass-by-name is equivalent to pass-by-value
– An array element, pass-by-name is like nothing else (example 1)
Trang 27Pass-by-Name: Example 2
// Assume k is a nonlocal variable
procedure sub1(x: int; y: int; z: int);
Trang 28Pass-by-Name: Jensen’s Device
real procedure SUM(ADDER, INDEX, LENGTH);
for INDEX := 1 step 1 until LENGTH do
TEMPSUM := TEMPSUM + ADDER;
SUM := TEMPSUM;
end;
If A is a scalar, then SUM(A, I, 100) produces …
Trang 29Pass-by-Name: Jensen’s Device
real procedure SUM(ADDER, INDEX, LENGTH);
for INDEX := 1 step 1 until LENGTH do
TEMPSUM := TEMPSUM + ADDER;
SUM := TEMPSUM;
end;
Trang 30Pass-by-Name: Jensen’s Device
real procedure SUM(ADDER, INDEX, LENGTH);
for INDEX := 1 step 1 until LENGTH do
TEMPSUM := TEMPSUM + ADDER;
SUM := TEMPSUM;
end;
If A is an array of 100 reals, then
SUM(A[I] * A[I], I, 100) returns …
Trang 31Pass-by-Name: Jensen’s Device
real procedure SUM(ADDER, INDEX, LENGTH);
for INDEX := 1 step 1 until LENGTH do
TEMPSUM := TEMPSUM + ADDER;
SUM := TEMPSUM;
end;
If A and B are arrays of 100 reals, then
Trang 32– The high cost of pass-by-name parameters, in
terms of execution efficiency
– It’s difficult to implement and can confuse both readers and writers of programs that use them
Trang 33a[1] = 3 a[2] = 1
Trang 34Parameter Passing Methods of Major
Languages
• Ada: All three semantic modes are available
– in: It’s the default mode
The actual parameter’s value cannot be changed
The formal parameter is a constant and allows only reading
– in out
The actual parameter’s value may be redefined
The formal parameter can be read and written
Trang 35Parameter Passing Methods of Major
pass-– The corresponding formal parameters can be pointers
to constants, which provide the efficiency of reference with in-mode semantics
pass-by-• Java
Trang 36Parameter Passing Methods of Major
Languages (cont.)
• C#
– Default method: pass-by-value
– Pass-by-reference is specified by preceding both a
formal parameter and its actual parameter with ref
• Fortran
– Always used the inout semantics model
– Before Fortran 77: pass-by-reference
– Fortran 77 and later: scalar variables are often
passed-by-value-result
• Pascal and Modula-2
– Default is pass-by-value
– Pass-by-reference is optional
Trang 37Type-Checking Parameters
• Software reliability demands that the actual
parameters be checked for consistency with the types of the corresponding formal parameters
• FORTRAN 77 and original C: none
• Pascal, FORTRAN 90, Java, and Ada: it is always required
• In C++, all functions must have their formal
parameters in prototype form, so type-checking
Trang 38Type-Checking Parameters (cont.)
• In ANSI C, the formal parameters of functions
can be defined in two ways
– The way of the original C: Using this method
avoids type checking
Trang 39Implementing Parameter Passing
• In most language parameter communication
takes place through the run-time stack
• Pass-by-value: copy value to the stack;
references are indirect to the stack
• Pass-by-result is implemented as the opposite of pass-by-value
• Pass-by-value-result can be implemented as a
combination of value and
pass-by-result
Trang 40Implementing Parameter-Passing (cont.)
communication to the callee is required,
inadvertent and erroneous changes may be
made to the actual parameter
Trang 41a = *addr_i
b = *addr_listi temp = a
Trang 42vs Pass-by-reference
int i = 3; // global void fun(int a, int b) {
Trang 43Stack Implementation of Parameter-Passing
Trang 44Multidimensional Arrays as Parameters
• If a multidimensional array is passed to a
subprogram and the subprogram is separately compiled, the compiler needs to know the
declared size of that array to build the storage mapping function
Trang 45Multidimensional Arrays as Parameters: C/C++
• Programmer is required to include the declared sizes of all but the first subscript in the actual
parameter
– Disallows writing flexible subprograms
void fun(int matrix[][3])
• Pass a pointer to the array and the sizes of the dimensions as other parameters; the user
should include the storage mapping function
#define a(r, c) (*(a + ((r) * num_cols) + (c)))
Trang 46Multidimensional Arrays as Parameters: Pascal
• Multidimensional array parameters are not a
problem in Pascal
– Pascal requires that array types be declared
type TblType = array[1 5, 1 10] of integer;
– A type name must be used in all formal
parameter declarations
procedure P(T : TblType);
Trang 47Multidimensional Arrays as Parameters: Ada
• Ada has constrained arrays (like Pascal) and
unconstrained array types
• An Ada unconstrained array type
type MATRIX_TYPE is array( INTEGER range <>,
INTEGER range <> ) of FLOAT; MATRIX_1 : MATRIX_TYPE (1 10, 1 20);
…
function SUMER(MAT: in MATRIX_TYPE) return FLOAT is
begin
for ROW in MAT‘range(1) loop
for COL in MAT‘range(2) loop
Trang 48Multidimensional Arrays as Parameters: FORTRAN
• Formal parameter that is array has a declaration after the header
– For single-dimension arrays, the subscript is
irrelevant
– For multi-dimensional arrays, the subscripts
allow the compiler to build the storage-mapping function
SUBPROGRAM SUB(MATRIX, ROWS, COLS, RESULT)
INTEGER ROWS, COLS
REAL MATRIX (ROWS, COLS), RESULT
…
END
Trang 49Multidimensional Arrays as Parameters: Java and C#
• Arrays are objects; they are all
single-dimensioned, but the elements can be arrays
• Each array inherits a named constant (length in Java, Length in C#) that is set to the length of
the array when the array object is created
float sumer(float mat[][]) {
float sum = 0.0f;
for (int row = 0; row < mat.length; row++) {
for (int col = 0; row < mat[row].length; col++) { sum += mat[row][col];
}
Trang 50Design Considerations for
Parameter Passing
• Two important considerations
– Efficiency
– One-way or two-way data transfer
• The above considerations are in conflict
– One-way (in) parameters are safe since they do
not allow aliases, but are expensive when large
data structures must be copied
– Pass-by-reference is fastest way to pass
structures of significant size and it allows aliasing – Functions should not allow reference parameters (functional side effects)
Trang 51a = b;
b = temp;
} swap(x, y); swap(&x, &y); swap(x, y);
swap(&i, &a[i]); swap(i, a[i]);
Pass-by-value Using pointer
parameters to Using reference parameters
Examples
Trang 52Parameters that are Subprogram
Names
• It is sometimes convenient to pass subprogram
names as parameters
• Issues:
1 Are parameter types checked?
2 What is the correct referencing environment for a subprogram that was sent as a parameter?
Trang 53Parameters that are Subprogram
Names: Parameter Type Checking
• C/C++: functions cannot be passed as
parameters but pointers to functions can be
passed; parameters can be type checked
• Later versions of Modula-2, and Fortran 90 do
• Later versions of Pascal and Ada does not allow subprogram parameters; a similar alternative is provided via Ada’s generic facility
• Java does not allow method names to be passed
Trang 54• In Pascal
procedure integrate(function func(x: real): real;
lowerbd, upperbd: real); begin
Trang 55Parameters that are Subprogram Names:
Referencing Environment vs Static Scope Rules
• Shallow binding: (callee) The environment of the
call statement that actives the passed subprogram
– Most natural technique for dynamically scoped
languages
• Deep binding: The environment of the subprogram that declares the passed subprogram
– Most natural technique for statically scoped languages
• Ad hoc binding: (caller) The environment of the
subprogram that passed the passed subprogram
Trang 56procedure SUB1; {deep}
Trang 57Parameters that are Subprogram Names:
Referencing Environment vs Dynamic Scope Rules
• Static scope rules: Referencing environment
depends on the lexical nesting of program blocks
in which names are declared
• Dynamic scope rules: Referencing environment
depends on the order in which declarations are
encountered at run time
– The definition of deep binding for statically scoped
languages has no more meaning
• Deep binding: The referencing environment of the
Trang 58Overloaded Subprograms
• An overloaded subprogram is one that has the
same name as another subprogram in the same referencing environment
– Every version of an overloaded subprogram has a unique protocol
• C/C++, Java, C#, and Ada include predefined
overloaded subprograms
– Example: Ada has several versions of the output function PUT The most common versions are
those that accept string, integer, and
floating-point type values as parameters