Title: Subprograms
1Subprograms
- Juan Carlos Guzmán
- CS 3123 Programming Languages Concepts
- Southern Polytechnic State University
2Subprogram
- Process abstraction
- Definition
- Header
- interface
- parameters
- also known as
- prototype
- signature
- Body
- implementation
- Call
- int fact(int x)
- if (x0)
- return 1
- else
- return xfact(x-1)
3Kinds of subprograms
- Procedure
- a collection of statements in a parameterized
computation - communication caller-callee (called subprogram)
using parameters, and data accessible by both - no meaningful return value
- Functions
- Same as procedures, but return values
4Parameter Passing
- Formal parameters
- those that appear in the subprograms header
- Actual parameters
- those that are used to call the function
- Positional vs keyword parameters
- (same issues as tuples vs. records)
5Parameter Passing Styles
- Direction of communication
- 1) actual parameters to formal parameters (in
mode) - 2) formal parameters to actual parameters (out
mode) - 3) both (inout mode)
- Communication between the caller and the callee
- a) at the onset of the call
- b) throughout the call
- c) when the call ends
6Parameter Passing Styles
- Pass by value (1a)
- the value of the parameter is accessible by the
callee at any time - Pass by result (2c)
- the value of the parameter is accessible by the
caller after the subprogram ends - Pass by value-result (3ac)
- the value of the parameter is accessible by the
callee at any time. Changes to that value will
only be known by the caller after the subprogram
ends - Pass by reference (3b)
- value of the parameter is shared between the
caller and the callee - Pass by name (3b)
- more complex than the rest...
7Examples
- swap(int x, int y)
- int z
- z x
- x y
- y z
-
- main()
- int a5
- int b7
- swap(a,b)
- printf("ad, bd",a,b)
- Pass by value
- a5, b7
- Pass by value-result
- a7, b5
- Pass by reference
- a7, b5
8Examples
- int a5
- f(int x)
- x
- printf("xd, ad,x,a)
-
- main()
- f(a)
- printf(", ad",a)
- Pass by value
- x6, a5, a5
- Pass by value-result
- x6, a5, a6
- Pass by reference
- x6, a6, a6
9Examples
- swap(int x, int y)
- int z
- z x
- x y
- y z
-
- main()
- int i0
- int A31,2,3
- swap(i,Ai)
- print(id, Ad",i,A)
- Pass by value
- i0, A1,2,3
- Pass by value-result
- i1, A0,2,3
- Pass by reference
- i1, A0,2,3
- Pass by name
- i1, A1,0,3
10Subprogram Implementation
- Compilation of programs written in high-level
languages usually have several segments at run
time - Code
- code (main, subprograms) resides here
- Global data
- all global declarations reside here
- persistent declarations (static) reside here
- Stack
- keeps data local to subprograms
- Heap
- keeps dynamically allocated data
11Stack
- The stack keeps info local to subprograms
- It is conformed of records
- Each record should contain
- arguments
- local variables
- temporaries
- control info
- Records are of different size!
Not accessible from the program
12Elements ofan Activation Record Instance
- Arguments
- fixed or variable number of arguments
- interface caller-callee
- Local Variables
- Temporaries
- Added by the compiler
- Control Information
- Where to find non-local data
- static link
- Previous ARI
- dynamic link
- Return address
- what to be executed after this routine ends
13Activation Records
Temporaries
Local Vars
Control Info
Static Link Dynamic Link
Pointer to Code
Arguments
14Sample Program
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
- ...
- D
- end C
- begin SAMPLE
- A(true)
- ...
- end SAMPLE
15Runtime Stack at ?
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
D
C
A
B
Heap
A
Sample
Static Link Dynamic Link
Pointer to Code
O/S
16Run of the Sample Program
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
Heap
Sample
Static Link Dynamic Link
Pointer to Code
?
O/S
17Run of the Sample Program
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
?
Heap
A
Sample
Static Link Dynamic Link
Pointer to Code
O/S
18Run of the Sample Program
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
?
B
Heap
A
Sample
Static Link Dynamic Link
Pointer to Code
O/S
19Run of the Sample Program
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
?
A
B
Heap
A
Sample
Static Link Dynamic Link
Pointer to Code
O/S
20Run of the Sample Program
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
C
A
B
Heap
A
?
Sample
Static Link Dynamic Link
Pointer to Code
O/S
21Run of the Sample Program
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
D
C
A
B
Heap
A
?
Sample
Static Link Dynamic Link
Pointer to Code
O/S
22Display
- Accessing nonlocal, nonglobal variables requires
several indirections through the static chain - The display solves this--it keeps all needed
environments in an array for easy access - There is no display element for global data
23Stack at ?, using Display
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
D
C
A
B
Heap
A
?
Display
Sample
Dynamic Link Pointer to Code
O/S
24Issues on Display
- The display size is computed at compile time--it
is the largest lexical depth in the program - The need for static chain is eliminated, but now
there is the need to keep the old display
pointer
25Stack at ?, using Display
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
Heap
Display
null
Sample
null
Prev. Display Ptr. Dynamic
Link Pointer to Code
?
O/S
26Stack at ?, using Display
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
?
Heap
A
Display
null
Sample
Prev. Display Ptr. Dynamic
Link Pointer to Code
O/S
27Stack at ?, using Display
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
?
B
Heap
A
Display
Sample
Prev. Display Ptr. Dynamic
Link Pointer to Code
O/S
28Stack at ?, using Display
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
?
A
B
Heap
A
Display
Sample
Prev. Display Ptr. Dynamic
Link Pointer to Code
O/S
29Stack at ?, using Display
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
C
A
B
Heap
A
Display
?
Sample
Prev. Display Ptr. Dynamic
Link Pointer to Code
O/S
30Stack at ?, using Display
- program SAMPLE
- procedure C forward
- procedure A(flag boolean)
- procedure B
- ...
- A(false)
- end B
- begin A
- if flag
- then B
- else C
- ...
- end A
- procedure C
- procedure D
- ... ??????? ?
- end D
- begin C
Globals
D
C
A
B
Heap
A
?
Display
Sample
Prev. Display Ptr. Dynamic
Link Pointer to Code
O/S
31Other Issues
- First activation record is treated specially
- it should contain info to return to the O/S
- global variables should not be put there--they
should be in the globals segment