Subprograms - PowerPoint PPT Presentation

About This Presentation
Title:

Subprograms

Description:

... vs pass-by-value-result. void main ... return cons (car (numList) , Filter (num, cdr (numList) ... pre: numList an infinite list of integers, with values = 2 ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 16
Provided by: jaime81
Learn more at: https://www.cs.uno.edu
Category:

less

Transcript and Presenter's Notes

Title: Subprograms


1
Subprograms
2
Subprograms
  • 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

4
Parameters
  • Formal and actual
  • Binding positional, name
  • Matching of formal with actual type, number,
    order
  • Optional parameters, indefinite-length argument
    list

5
execution 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.

6
Parameter 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)

7
Pass-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)

8
void 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
9
Expression evaluation
  • Order of expression evaluation
  • Argument evaluation eager, delayed, lazy
  • Example
  • f(ab/c, a (b c))

10
Resolving 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.

11
Resolving 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.

12
Procedure 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
13
Program 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)

14
Example 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)))
15
Example 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))
Write a Comment
User Comments (0)
About PowerShow.com