Title: Subprograms
1Subprograms
- Jianhua Yang
- Department of Math and Computer Science
- Bennett College
2Outline
- Fundamentals of Subprograms
- Design issues
- Local Referencing Environments
- Parameter-Passing Methods
- Parameters that are subprogram names
- Overloaded Subprograms
- Generic Subprograms
- Design Issues for functions
- User-defined overloaded Operators
- Coroutines
39.1 Fundamentals of Subprograms
- Characteristics
- Single entry point
- Caller suspended
- Return to caller
41. Basic Definition
- A subprogram definition describes the interface
to and the actions of the subprogram abstraction. - Procedure
- Function
5Subprogram header
- 1. it is a subprogram with a type
- 2. specifying its name
- 3. specifying a list of parameters
Fortran Subroutine Adder (parameters) Ada
procedure Adder (parameters) C-base
language void adder (parameters)
6Parameter profile
- It contains the number, order, and types of its
formal parameters.
72. Parameters
- Formal parameters
- Actual parameters
- Positional parameters
- Keyword parameters
Sumer (Length gt My_Length, List gt
My_Array, Sum gt My_Sum)
8Default value for formal parameters
- Ada function header
- function Compute_Pay (IncomeFloat
- Exemptions Integer 1
- Tax_Rate Float) return Float
Call this function Pay Compute_Pay(2000.0,Tax_
Rategt0.15)
9C example
- float compute_pay (float income, float tax_rate,
int exemptions 1)
The default parameters must appear last.
Call this function pay compute_pay(2000.0,
0.15)
103. Procedures and Functions
- They are two distinct categories of subprograms
11Procedure
- Procedures are collections of statements that
define parameterized computations.
12Interaction between procedure and its caller
- 1. Visible variables in both a procedure and the
caller can be changed by the procedure. - 2. if a procedure has formal parameters that
allow the transfer of data to the caller, those
parameters can be changed.
13Function
- Different from a procedure, it modifies
- Neither its parameters
- Nor any variables defined outside the function.
- Functions are called by appearances of their
names in expressions, along with the required
actual parameters. - Return a value to the calling code (caller).
14Function example in C
- float power (float base, float exp)
- Called with
- result 3.4 power(10.0, 3)
Functions can be used as a procedure
159.2 Design Issues for Subprograms
- Parameter-passing method
- Type check
- Local variables allocation statically or
dynamically - Nested definition
- Subprogram as parameter
- Overload
- Generic.
169.3 Local referencing environments
- Variables that are defined inside subprograms are
called local variables - The scope is usually the body of the subprogram
in which they are defined.
17Local variables
They are bound to storage when the subprogram
begins execution and are unbound from storage
when that execution terminates.
18Stack-dynamic
- Ad.
- Flexibility
- Space share
- Da.
- Time cost
- Indirect access
- Non history-sensitive
19Static
- Ad.
- Efficiency
- No run-time overhead
- Da.
- Inability to support recursion
- Space share problem
20Example
- int adder (int list, int listlen)
- static int sum0
- int count
- for(count0 count lt listlen count)
- sum listcount
- return sum
219.4 Parameter-Passing Methods
- Parameter-passing methods are the ways in which
parameters are transmitted to and/or from called
subprograms.
221. Semantics Models of Parameter Passing
- In mode
- Out mode
- In-out mode
232. Implementation models of parameter passing
- Pass-by-value
- Pass-by-result
- Pass-by-value-result
- Pass-by-reference
- Pass-by-name
241) pass-by-value
Callee Void sub (int x, int y, int z)
Caller sub (a, b, c)
In mode
Out mode
Inout mode
25Disadvantages
- 1. space cost
- 2. large data transfer cost too much time
262) pass-by-result
- It is an implementation model for
out-mode-parameters. - The same disadvantages as 1).
273) Pass-by-value-result
- This one is in effect a combination of
pass-by-value and pass-by-result. - Also called pass-by-copy.
- The same disadvantages as the previous ones.
284) Pass-by-reference
- Rather than copying data values back and forth,
this method transmits an access path, usually
just an address, to the called subprogram. - Its advantage is its efficiency in terms of time
and space. - Its disadvantages
- 1. indirect accessing
- 2. inadvertent errors
- 3. aliases problem
29Example
void sub(int param)
- void main()
- int global
-
- sub(global)
-
Memory block
305)pass-by-name
313. Multidimensional Arrays as Parameters
- void fun (int matrix10)
-
-
- Void main()
- int mat510
-
- fun (mat)
-
32Examples of parameter passing
void swap1( int a, int b) int
tempa ab btemp
void swap2( int a, int b) int
tempa ab btemp
swap2(c, d)
swap1(c, d)
339.5 Parameters that are subprogram names
- In C, and C, function name can not be used as a
parameter - But we can use a pointer point to a function.
349.6 Overloaded Subprograms
- Overloaded operator is one that has multiple
meanings. - Overloaded subprogram is a subprogram that has
the same name as another subprogram in the same
referencing environment. - The meaning of a call to an overloaded subprogram
is determined by the actual parameter list.
35example
- void fun (int, int)
- void fun (float)
- void fun()
369.7 Generic subprograms
- Purpose is to increase the reusability of
software - A generic or polymorphism takes parameters of
different types on different activations.
371. Generic Functions in C
- Generic functions in C have the descriptive
name of template functions. - General form
- template lttemplate parametersgt
- A template parameter has one of the following
forms - class identifier
- typename identifier
38Example
- template ltclass Typegt
- Type max (Type first, Type second)
- return first gt second ? First second
-
int max (int first, int second) return first gt
second ? first second
39Generic sort example
template ltclass Typegt void generic_sort ( Type
list, int len) int top, bottom Type
temp for (top0 topltlen-2 top) for
(bottom top 1 bottom ltlen -1 bottom) if
(listtop gt listbottom) temp
listtop listtop listbottom listbo
ttom temp
float flt_list100 generic_sort(flt_list,
100)
409.8 Coroutines
- A special kind of subprogram
- Might have multiple entry-points
- The invocation of a coroutine is called a resume
rather than a call - Only one coroutine actually executes at a given
time - Quasi-concurrency
41example
A
B
resume B resume B resume B
resume A resume A
42Summary
- Subprogram definitions
- Local referencing environments
- Parameter passing
- Overloaded subprograms
- Generic subprograms
- Coroutines.