PL 12 CH 9
PL 12 CH 9
Subprograms
ISBN 0-321-49362-1
Chapter 9 Topics
• Introduction
• Fundamentals of Subprograms
• Design Issues for Subprograms
• Local Referencing Environments
• Parameter-Passing Methods
• Parameters That Are Subprograms
• Calling Subprograms Indirectly
• Design Issues for Functions
• Overloaded Subprograms
• Generic Subprograms
• User-Defined Overloaded Operators
• Closures
• Coroutines
1-16
Local Referencing Environments: Examples
list 1 - in mode
list 2 - inout mode
intermediate list for addition - out mode
- Another issue:
- In C, it is possible
• 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
• Implementation requires that the
referencing environment of the caller is
passed with the parameter, so the actual
parameter address can be calculated
• C++
– Pass-by-value
– A special pointer type called reference type for pass-by-reference
• Java
– All non-object parameters are passed are passed by value
So, no method can change any of these parameters
non-object parameters are basically primitive types - int, double,
char, boolean, etc.
– Object parameters are passed by reference
• 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
1-41
Design Considerations for Parameter
Passing
• C++
– Versions of a generic subprogram are created
implicitly when the subprogram is named in a
call or when its address is taken with the &
operator
– Generic subprograms are preceded by a template
clause that lists the generic variables, which can
be type names or class names
• Java 5.0
- Differences between generics in Java 5.0 and
those of C++:
1. Generic parameters in Java 5.0 must be classes
2. Java 5.0 generic methods are instantiated just
once as truly generic methods
3. Restrictions can be specified on the range of
classes that can be passed to the generic method
as generic parameters
4. Wildcard types of generic parameters
• C# 2005
- Supports generic methods that are similar to
those of Java 5.0
- One difference: actual type parameters in a call
can be omitted if the compiler can infer the
unspecified type
– Another – C# 2005 does not support wildcards
• F#
– Infers a generic type if it cannot determine the
type of a parameter or the return type of a
function – automatic generalization
– Such types are denoted with an apostrophe and
a single letter, e.g., ′a
– Functions can be defined to have generic
parameters
let printPair (x: ′a) (y: ′a) =
printfn ″%A %A″ x y
- %A is a format code for any type
- These parameters are not type constrained
Copyright © 2018 Pearson. All rights reserved. 1-56
Generic Subprograms (continued)
• F# (continued)
– If the parameters of a function are used with
arithmetic operators, they are type constrained,
even if the parameters are specified to be
generic
– Because of type inferencing and the lack of type
coercions, F# generic functions are far less
useful than those of C++, Java 5.0+, and C#
2005+
• A JavaScript closure:
function makeAdder(x) {
return function(y) {return x + y;}
}
...
var add10 = makeAdder(10);
var add5 = makeAdder(5);
document.write(″add 10 to 20: ″ + add10(20) +
″<br />″);
document.write(″add 5 to 20: ″ + add5(20) +
″<br />″);
- The closure is the anonymous function returned
by makeAdder
- The lifetime of y is the whole program run time. 1-60
Closures (continued)
• C#
- We can write the same closure in C# using a nested
anonymous delegate
- Func<int, int> (the return type) specifies a delegate
that takes an int as a parameter and returns and int
static Func<int, int> makeAdder(int x) {
return delegate(int y) {return x + y;};
}
...
Func<int, int> Add10 = makeAdder(10);
Func<int, int> Add5 = makeAdder(5);
Console.WriteLine(″Add 10 to 20: {0}″, Add10(20));
Console.WriteLine(″Add 5 to 20: {0}″, Add5(20));
1-65
Coroutines Illustrated: Possible
Execution Controls