ch9 2
ch9 2
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
– In Python, function definitions are executable; in all other languages, they are non-executable. For
example:
if …
def fun(…):
else
def fun(…):
…
• A subprogram call is an explicit request that the subprogram be executed.
• A subprogram header is the first part of the definition, including the name, the kind of
subprogram, and the formal parameters. For example:
• The parameter profile (aka signature) of a subprogram is the number, order, and types of its
parameters.
• The protocol is a subprogram’s parameter profile and, if it is a function, its return type.
Copyright © 2007 Addison-Wesley. All rights reserved. 1-5
Basic Definitions (continued)
– C, C++, Perl and JavaScript allow the number of actual parameters to be fewer than the
number of formal parameters.
– C# allows a variable number of parameters as long as they are of the same type. For
example:
public void DisplayList ( params int [] list ) {
foreach (int next in list ) {
Console.WriteLine(“Next value {0}”, next);
}
}
Call examples:
myObject.DisplayList(myList);// myList is an int array.
myObject.DisplayList(2, 4, 3 * x – 1, 17);
- Ruby and Python also support variable numbers of parameters. In Ruby, the actual
parameters are sent as elements of a hash literal and the corresponding formal parameter
is preceded by an asterisk.
- In Python, the actual is a list of values and the corresponding formal parameter is a name with
an asterisk
Copyright © 2007 Addison-Wesley. All rights reserved. 1-9
Variable number of parameters
tester('first', mon => 72, tue => 68, wed => 59, *list)
Inside tester, the values of its formal parameters are as follows:
p1 is 'first'
p2 is {mon => 72, tue => 68, wed => 59}
p3 is 2
p4 is [4, 6, 8]
Python supports parameters that are similar to those of Ruby.
• Advantages
– Comparatively efficient – require no
allocation/de allocation overhead
– History sensitive
• Disadvantages
– Cannot support recursion
– Cannot share storage with local variables of
other inactive subprograms
• In this PL/I example, Count is stack-dynamic due to the fact that the
subroutine is marked as Recursive, whereas, Sum is so due to the
keyword Save.
Non-static version:
int adder(int list[], int listlen) {
int sum = 0;
int count;
for (count = 0; count < listlen; count++)
sum += list[count];
return sum;
}
Copyright © 2007 Addison-Wesley. All rights reserved. 1-16
Nested Subprograms
• The corresponding formal parameter acts as a local variable, with its value
transmitted to caller’s actual parameter when control is returned to the caller,
by a physical move.
• If values are returned by access path, then there is the problem of ensuring
that the initial value of the actual parameter is not used in the called
subprogram. The difficulties of this lead to values returned by copy method.
What is the value of ‘a’ upon return from the function call.
• Depends upon the implementation. Which of the copies from formal
to actual occurs last.
• Since this order may vary by different implementations, the
results of the same program may also vary.
– If called at the beginning then 17 will be returned
– If called at the return from the subprogram then 35 is returned
1-32
Parameter Passing methods
• { c: array [1..10] of integer;
m,n : integer;
procedure r (k,j : integer)
begin
k := k+1; m := m + 1
j := j+2; c[m] := c[m] + 2
end r;
/* set c[n] to n */
m := 2;
r(m,c[m]); m c[ ]
write m,n; 2 1 2 3 4 5 6 7 8 9 10
} 3 1 2 5 4 5 6 7 8 9 10
m:= m+1
1-33
Pass-by-Name (Inout Mode)
• call by value: copy going into the procedure
• call by result: copy going out of the procedure
• call by value result: copy going in, and again going out
• call by reference: pass a pointer to the actual parameter, and
indirect through the pointer
1-34
Parameter Passing Methods of Major
Languages
• Pascal provides for two:
– X: integer is a pass-by-value
– Var X: integer is a pass-by-reference
1-35
Parameter Passing Methods of Major
Languages (continued)
• 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
• declared in can be referenced but not assigned
• declared in out parameters can be referenced and assigned
– Example: Procedure Adder (A: in out Integer; B : in Integer; C : out Float)
• Fortran 95
- Parameters can be declared to be in, out, or inout mode
– Example: Subroutine Adder(A, B, C)
Integer, Intent(Inout) :: A
Integer, Intent(In) :: B
Integer, Intent(out) :: C
• C#
- Default method: pass-by-value
– Pass-by-reference is specified by preceding both a formal parameter and its actual parameter
with ref
– Example: void sumer (ref int oldSum, int newOne) {..}
call is sumer(ref sum, newValue);
• 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
def fibonacci(last)
first, second = 1, 1
while first <= last
yield first
first, second = second, first + second
end
end