Subprograms - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Subprograms

Description:

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 ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 32
Provided by: juancarl
Category:

less

Transcript and Presenter's Notes

Title: Subprograms


1
Subprograms
  • Juan Carlos Guzmán
  • CS 3123 Programming Languages Concepts
  • Southern Polytechnic State University

2
Subprogram
  • 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)

3
Kinds 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

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

5
Parameter 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

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

7
Examples
  • 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

8
Examples
  • 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

9
Examples
  • 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

10
Subprogram 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

11
Stack
  • 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
12
Elements 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

13
Activation Records
Temporaries
Local Vars
Control Info
Static Link Dynamic Link
Pointer to Code
Arguments
14
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
  • ...
  • D
  • end C
  • begin SAMPLE
  • A(true)
  • ...
  • end SAMPLE

15
Runtime 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
16
Run 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
17
Run 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
18
Run 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
19
Run 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
20
Run 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
21
Run 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
22
Display
  • 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

23
Stack 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
24
Issues 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

25
Stack 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
26
Stack 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
27
Stack 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
28
Stack 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
29
Stack 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
30
Stack 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
31
Other 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
Write a Comment
User Comments (0)
About PowerShow.com