Subroutines (Part 1)
CSE 262, Spring 2003
© 2003 G. Drew Kessler and
William M. Pottenger
1
Subroutines
Method of process abstraction
 Supports modular design
 Improves readability
Allows code reuse
 Conserves memory
 Saves coding time
Characteristics:
 Single Entry
 Calling program blocks during subroutine call
 Control always returns to caller
 Except: co-routines, concurrent execution
© 2003 G. Drew Kessler and William M. Pottenger
2
Design Issues & Mechanisms
Method of parameter passing
Function vs. procedure
 Mathematical function vs. program statement
Local and non-local referencing environments
Overloaded subroutines
Generic subroutines
Separate compilation, independent compilation
 Require info for type checking separately compiled
subroutines?
Aliasing and side effect problems
© 2003 G. Drew Kessler and William M. Pottenger
3
Subroutine Parameters
Parameters
 Provide access to data outside of subroutine
 ‘Safer’ than using global variables
 Allows for specialization of computational tasks
Actual parameters: values given at subprogram call
site
Formal parameters are given in subprogram definition
© 2003 G. Drew Kessler and William M. Pottenger
4
Subprogram Definition
Subprogram header: first line of definition
Provides name, list of parameters (0 or more)
 May use special word (“procedure”, “function”,
“subroutine”), or context (as in C)
Parameter profile: provides #, order, and types of
formal parameters
Parameter protocol: profile plus subroutine return type
Subprogram declaration or prototype: provides info
needed by compiler to do type-checking, before
definition (some languages need forward qualifier)

© 2003 G. Drew Kessler and William M. Pottenger
5
Parameter Designs
Association between formal and actual parameters:

Positional parameters
void sort(int list[], int size, bool ascending);
sort(numbers, n, true); // call to sort()

Keyword parameters
sort(size=>n, list=>numbers, ascending=>true); //call
Default parameters?
void sort(int list[], int size=100, bool ascending=true);
sort(numbers); // call to sort()
Parameter list length fixed? (In C++, “...” is 0 or more)
void sort(int list[], …); // avoid type checking of …
sort(numbers, size); // call to sort()
sort(…); // Will this work? What does it mean?
What commonly used C function employs this ellipsis
operator? How does it work?
Parameters (& subroutine parameters) type-checked?
© 2003 G. Drew Kessler and William M. Pottenger
6
Local and Non-Local Referencing
Environments
Local variables defined inside subroutine
Formal parameters are usually local variables
Non-local variables are either global or are variables
that are in scope but local to another subroutine
Local variable storage

Static
 Allows direct access
 Provides for history sensitive subroutines

Stack-dynamic
 Allows recursion
 Provides memory savings
© 2003 G. Drew Kessler and William M. Pottenger
7
Parameter Passing Methods
Semantic modes:
 In, out, in out
Implementation methods of data transfer:
 value, access path (a.k.a. reference)
Methods:
 Pass-by-value (in)
 Pass-by-result (out)
 We’re not referring to the subroutine’s return value here!
 Pass-by-value-result (in out, value)
 (a.k.a. pass-by-copy)
 Pass-by-reference (in out, reference)
 Pass-by-name (in out, other)
© 2003 G. Drew Kessler and William M. Pottenger
8
Parameter Passing Examples
Caller
Actual parameter
Subroutine
Formal parameter
Pass-by-value
Pass-by-result
Pass-by-valueresult
Pass-by-reference
© 2003 G. Drew Kessler and William M. Pottenger
9
Parameter Passing in Various Languages
Wide variety of implementations
 Fortran: always by reference
 Pascal: programmers choice (var)
 C: by value (except for arrays/explicit
pointers)
 C++: by value, C++ ‘reference’ or as with C
 Java: ‘built-ins’ by value; others by reference
© 2003 G. Drew Kessler and William M. Pottenger
10
Parameter Passing in Ada
In, In/Out, Out:
 Ada parameter
passing can be
implemented
by value or
reference…
 Morgan Kaufmann (Figure
reproduced by permission of
author/publisher)
© 2003 G. Drew Kessler and William M. Pottenger
11
Call-by-Name Example (ALGOL 60)
real procedure sum ( k, l, u, ak)
value l, u;
integer k, l, u;
real ak;
begin
real s; s := 0;
for k := l step 1 until u do
s := s + ak
end
Use:
x := sum (i, 1, n, V[i])
n
x
V
i
i 1
x := sum (i, 1, m, sum(j, 1, n, A[i, j]))
m
(Jensen’s Device)
© 2003 G. Drew Kessler and William M. Pottenger
x
n

i 1
j 1
Ai , j
12
Call-by-Name Problem
Consider a swap routine:
procedure swap (j, k)
integer j, k;
begin
integer t;
t := j;
j := k;
k := t;
end;
And, consider this:
i := 1, A[1]:=2, A[2]:=8
swap(i, A[i])
Do other methods have a problem with this?
© 2003 G. Drew Kessler and William M. Pottenger
13
Overloaded subprograms
System and user-defined
Same name, different operation
Right operation identified by parameter and return
types
 Return type distinction not available if mixed-mode
expressions allowed
 For example:
int f (int i) {…;}
float f (int i) {…;}
int i = j = 7;
float result = f(i) + f(j); // Which f() is called?
© 2003 G. Drew Kessler and William M. Pottenger
14
Generic subroutines
Provide routine, where parameter and/or variable
types are defined at a later time.
New instance of routine with specific type created
when needed or explicitly
Ada generic units
C++ templates
template <class Type>
Type findMax(Type list[], int size) {
Type max = list[0];
for (int i=1; i<size; i++)
if (list[i]<max) max = list[i];
return max;
}
© 2003 G. Drew Kessler and William M. Pottenger
15
Generic Subroutines/Modules
 Morgan Kaufmann (Figure
reproduced by permission of
author/publisher)
Is it wise to return item by value?
© 2003 G. Drew Kessler and William M. Pottenger
16
Inline Expansion
• C++ and Ada support ‘inline expansion’
• C++:
inline int max (int a, int b) {
return a > b ? a : b;
}
• Ada:
pragma inline (max);
• Avoids potential problems with macros: e.g.,
– #define MAX(a,b) ((a) > (b) ? (a) : (b))
– Doesn’t work when ‘calling’ MAX(x++,y++)
• Costs more – increase in code size/compile time
© 2003 G. Drew Kessler and William M. Pottenger
17
CoRoutines
© 2003 G. Drew Kessler and William M. Pottenger
18
Descargar

Subroutines (Part 1)