Lec 4 Subprograms
Lec 4 Subprograms
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
• Overloaded Subprograms
• Generic Subprograms
• Design Issues for Functions
• User-Defined Overloaded Operators
• Coroutines
def fibonacci(last)
first, second = 1, 1
while first <= last
yield first
first, second = second, first + second
end
end
• In mode
• Out mode
• Inout mode
• 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
• 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
• Ada
– Versions of a generic subprogram are created
by the compiler when explicitly instantiated by
a declaration statement
– Generic subprograms are preceded by a
generic clause that lists the generic variables,
which can be types or other subprograms
• 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++ and Ada:
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