Sub Prog
Sub Prog
Subprograms
ISBN 0-321-33025-0
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
9-2
1
Introduction
9-3
Fundamentals of Subprograms
9-4
2
Basic Definitions (1/3)
9-5
9-6
3
Basic Definitions (3/3)
… formal
caller d = f1(a, b, c) parameter
Actual/Formal Parameter
Correspondence (Positional)
• Positional
– 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
– Safe and effective
…
caller d = f1(a, b, c)
4
Actual/Formal Parameter
Correspondence (Keyword)
• Keyword
– The name of the formal parameter to which an
actual parameter is to be bound is specified
with the actual parameter
In Ada
Sumer (Length => My_Length,
List => My_Array,
Sum => My_Sum);
9-9
Mixed Mode
• In Ada Positional
Sumer (My_Length,
Sum => My_Sum
List => My_Array,
);
Keyword
9-10
5
Formal Parameter Default Values
call
pay = compute_Pay(20000.0, 0.15);
6
Variable Number of Parameters in C#
9-13
9-14
7
Design Issues for Subprograms
9-15
9-16
8
Local Referencing Environments
• Local variables
#include <stdio.h>
can be static
static
– More efficient int sum; stack-dynamic
(no indirection)
– No run-time int fun1(int a, float b)
overhead {
static int cnt;
– Cannot support int i, j;
recursion … static
} stack-dynamic
9-17
9-18
9
Semantics Models of Parameter Passing
In Mode
Out Mode
Inout
Mode
9-19
9-20
10
Illustrating Pass-by-Value
caller
… int f1(int x, float y, int z) {
d = f1(a, b, c) …
push(c) x
a
push(b)
b y
push(a) c z
stack
9-21
9-22
11
Pass-by-Result: C# Examples
9-24
12
Pass-by-Reference (for Inout Mode)
9-25
definition
call
fun(total, total)
call (if i == j)
fun(list[i], list[j])
9-26
13
Example of Creating Alias in C
int * global;
void main() {
…
sub(global);
…
}
void sub(int * param) {
…
} global and
param are aliases
9-27
• By textual substitution
• Formals are bound to an access method at
the time of the call, but actual binding to a
value or address takes place at the time of
a reference or assignment
• Allows flexibility in late binding
9-28
14
Pass-by-Name: Resulting Semantics
sub1(k+1, j, i);
補充教材
9-30
15
Implementing Parameter-Passing
Methods
Passed by value
Passed by result
Passed by value-result
Passed by reference
9-32
16
Parameter Passing Methods of Major
Languages (Fortran and C)
• 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
•C
– Pass-by-value
– Pass-by-reference is achieved by using pointers
as parameters
9-33
• C++
– A special pointer type called reference type for
pass-by-reference
• Java
– All parameters are passed by value
– Object parameters are passed by reference
9-34
17
Parameter Passing Methods of Major
Languages (Ada)
• Ada
– Three semantics modes of parameter
transmission: in, out, in out; in is the
default mode
– 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
9-35
• C#
– Default method: pass-by-value
– 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 @_
9-36
18
Type Checking Parameters
9-37
19
In C99 and C++
9-39
9-40
20
Multidimensional Arrays as Parameters:
C and C++
• Programmer is required to include the
declared sizes of all but the first subscript
in the actual parameter
void main() {
int mat[5][10]; for the storage
… mapping function
fun(mat); Note: C array is row-major
…
} 9-41
21
Multidimensional Arrays as Parameters:
Ada
• Constrained arrays: Not a problem;
declared size is part of the array’s type
• Unconstrained arrays
– index ranges are not given in the array type
definition.
– can be formal parameters ⇒ cause problems?
type Mat_Type is array (Integer range <>,
unconstrained Integer range <>) of Float;
array type
…
function Sumer(Mat : in Mat_Type) return Float
…
9-43
Ada’s Solution
• Definitions of variables of unconstrained array
types must include index ranges.
– Subprogram can obtain the index range
information of the actual parameter
type Mat_Type is array (Integer range <>,
Integer range <>) of Float;
Mat_1 : Mat_Type(1..100, 1..20);
index range
function Sumer(Mat : in Mat_Type) return Float
is
begin
for Row in Mat’range(1) loop
for Col in Mat’range(2) loop get ranges
… 9-44
22
Multidimensional Arrays as Parameters:
Fortran
• Formal parameter that are arrays have
a declaration after the header
– For single-dimension arrays, subscripts
in the declaration are irrelevant
– For multi-dimensional arrays, the
subscripts allow the compiler to build the
storage-mapping function
9-45
Fortran Example
array declaration
9-46
23
Multidimensional Arrays as Parameters:
Java and C#
• Similar to Ada
• 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
9-47
Example in Java
row的大小
float sumer(float mat[ ][ ]) {
float sum = 0.0f;
for (int row = 0; row < mat.length, row++) {
for (int col = 0; col < mat[row].length; col++ {
sum += mat[row][col];
}
} column的大小
return sum;
}
9-48
24
Design Considerations for Parameter
Passing
9-50
25
Parameters that are Subprogram Names:
Parameter Type Checking
• C and C++
– functions cannot be passed as parameters, but
pointers to functions can be passed
– The type of the pointer is the function’s
protocol accepting a
pointer to function
– So parameters can be type checked
int fun1(int x, float y);
… int fun2(int (*fc)(int, float))
{
int (*p)(int,float); fc(7, 12.3);
p is a pointer return 0;
p = fun1;
fun2(p); to function }
passing p as a parameter 9-51
9-52
26
Parameters that are Subprogram
Names: Referencing Environment
• What is the referencing environment of the
subprogram that is passed as a parameter?
• Shallow binding: The environment of the
call statement that enacts the passed
subprogram
• Deep binding: The environment of the
definition of the passed subprogram
• Ad hoc binding: The environment of the call
statement that passed the subprogram
9-53
27
Overloaded Subprograms
9-56
28
Generic Subprograms
9-57
Examples of parametric
polymorphism: C++
template <class Type>
Type max(Type first, Type second) {
return first > second ? first : second;
}
• The above template can be instantiated for any
type for which operator > is defined. For example
29
Design Issues for Functions
User-Defined Overloaded
Operators
9-60
30
Coroutines
• A coroutine is a subprogram that has multiple
entries and controls them itself
• Also called symmetric control: caller and called
coroutines are on a more equal basis
• A coroutine call is named a resume
• The first resume of a coroutine is to its beginning,
but subsequent calls enter at the point just after
the last executed statement in the coroutine
• Coroutines repeatedly resume each other, possibly
forever
• Coroutines provide quasi-concurrent execution of
program units (the coroutines); their execution is
interleaved, but not overlapped
9-61
9-62
31
Coroutines Illustrated: Possible
Execution Controls
9-63
9-64
32
Summary
9-65
33