Title: Subprograms
1Subprograms
2Subprograms
- Used as a tool for decomposition
- Sometimes can be translated separately and
combined after translation. - Independent compilation
- Separate compilation
- Two flavors procedures, functions
- Variables used local, non-local or global
- L-value reference to memory which stores routine
body - R-value execution of the routines body.
- Functions as variables, pointer to functions
int(ps)(int) ps sum int I (ps)(5) - A dynamic binding policy.
3- Declaration and definition of routine.
- At execution time routine instance
- code segment and activation record
4Parameters
- Formal and actual
- Binding positional, name
- Matching of formal with actual type, number,
order - Optional parameters, indefinite-length argument
list
5execution environment of a function
- local environment
- plus its non-local environment determined by the
time of its call from the state of the Stack of
activation records. - Closure a function activation record along with
its non-local environment.
6Parameter passing
- Call-by-value copy-in
- Does not modify argument.
- Call-by-result copy-out
- Argument modifiers
- Call-by-value-result copy-in, copy-out
- Call-by-reference
- Call-by-pointer (simulation of previous)
7Pass-by-reference (aliases) vs pass-by-value-resul
t
- int i 3
- void fun( int a, int b)
- i b
-
- void main()
- int list10
- listi5
- fun(i, listi)
- printf( d and d, i, listi)
8void main() int value 2, list 1,3, 5,
7,9 swap(value, list0) swap(list0,
list1) swap(value, listvalue) void
swap(int a, int b) int temp temp a
a b b temp
9Expression evaluation
- Order of expression evaluation
- Argument evaluation eager, delayed, lazy
- Example
- f(ab/c, a (b c))
10Resolving references
- When functions use only only data types as
parameters we know how to resolve reference to
variables in the execution of the body of the
function - i. find name in local environment
- else follow static/dynamic chain of activation
records looking for the name. - Which chain to follow depends on the scope
disciplined used.
11Resolving references
- The situation becomes more complex when we allow
functions as parameters - this implies that the function may not invoked in
the same environment where it was defined. In
other words we need to come up with a method to
determine the "non-local environment". - Most languages choose one of following
- i. non-referencing environment enviroment of
definition of function. - ii. non-referencing environment environment
where the function is passed as a parameter. - iii.non-referencing environment environment
where function is invoked. (not a natural choice) - see example and you get 3 different answers.
12Procedure SUB1 var x INTEGER procedure
SUB2 // declaration of SUB2 begin
write(x, x) end procedure
SUB3 var x INTEGER begin
x 3 SUB4(SUB2) //passing SUB2 as
parameter end procedure SUB4( procedure
X) var x integer begin
x 4 X // invoking SUB2
end begin --- of SUB1 x 1
SUB3 end
13Program development organization
- monolithic
- Modules here becomes important to have a
separation of a module's specification from its
implementation for programming in the large. - When separated into modules an important issue
come up - compilation order
- and dependecy of a module on other modules
- independent compilation
- separate compilation (based on dependency)
14Example of use of Lazy evaluation Infinite List
// definition of an infinite list // pre n an
integer number // post a list of all integer
numbers starting from n From (n) return
cons (n, (from ( n 1)) // removes
multiples of num in numList // pre num an
integer numList an infinite list of numbers //
post returns a list formed by all entries in
numList which are not // // multiples of
num Filter (num , numList) if ( car (numList)
num 0) return Filter(num,
cdr(numList)) else return cons
(car (numList) , Filter (num, cdr (numList)))
15Example of Lazy evaluation List of all primes
//returns all prime numbers in a given infinite
list // pre numList an infinite list of
integers, with values gt 2 // post a list
consisting of all elements in numList which are
not multiple of any of the others Sieve(numList)
return cons ( car (numList), sieve( filter
(car (numList), cdr(numList))) allPrimes
Sieve(from (2))