Subprograms - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Subprograms

Description:

Execution of the calling program is suspended while the called subprogram executes ... void sumer( ref int oldSum ) Pass-by-Name ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 24
Provided by: wilhelmin
Category:

less

Transcript and Presenter's Notes

Title: Subprograms


1
Chapter 9
  • Subprograms

2
What are subprogram?
  • Fundamental building blocks of programs
  • Reuses code resulting in savings in memory space
    and coding time
  • A process abstraction

3
Characteristics of Subprograms
  • Single entry point
  • Execution of the calling program is suspended
    while the called subprogram executes
  • Control always returns to the caller when
    subprogram execution terminates

4
Basic Definitions (1)
  • Subprogram declaration
  • Prototype
  • Acts a interface to the subprogram by providing
    information on parameter type
  • Required in languages where the compiler must
    translate calls to the subprogram before the
    subprogram is actually defined
  • Compiler needs the information in the declaration
    to perform static type checking

5
Basic Definitions (2)
  • Subprogram definition
  • Describes the interface and actions performed by
    the subprogram
  • Subprogram header
  • First line of the subprogram
  • Performs the following actions
  • Specifies that the succeeding lines of code is a
    subprogram
  • Defines the name of the subprogram
  • Lists the subprogram parameters, if any
  • Subprogram body
  • Lines of code that constitutes the actions
    performed by the subprogram

6
Basic Definitions (3)
  • Subprogram call statements
  • Makes available the arguments required by the
    subprogram parameters and explicitly requests the
    subprogram to execute.

7
Ways for a subprogram to access data
  • Through formal parameters
  • Non-local data whose scope is determined either
    statically or dynamically

8
Subprogram Parameter
  • Parameter profile
  • Number, order and types of formal parameters to
    the subprogram
  • Formal Parameters
  • Provide a means for the subprogram to gain access
    to data it needs to execute

9
Types of parameters (1)
  • Positional
  • The first actual parameter is bound to the first
    formal parameter, the second actual parameter is
    bound to the second formal parameter and so on
  • Most commonly used
  • Ex
  • procedure sum( xinteger yinteger var
    zinteger)
  • sum(a, b, c)

10
Types of parameters (2)
  • Keyword
  • Name of the formal parameter to which the actual
    parameter is to be bound is specified with the
    actual parameter
  • Ex
  • procedure sum( xinteger yinteger var
    zinteger)
  • This sum(xgta, ygtb, zgtc)
  • Is the same as sum(xgta, ygtb, zgtc)

11
Language Implementation of Parameters
  • Ada and Fortran 90
  • Allow a mix of the two parameter types in their
    call statements
  • C, Fortran 90 and Ada
  • Formal parameters can have default values

12
Local Variables
  • Local referencing environments
  • C/C example
  • int adder(int list , int listlen)
  • static int sum 0 // static variable
  • int count // stack dynamic variable
  • for (count 0 count lt listlen count)
  • sum list count
  • return sum

13
Local Variable Types
  • Static
  • Allocated at compile time. Deallocated when the
    program terminates
  • Stack dynamic
  • Memory is allocated when the subprogram starts
    execution and deallocated when the subprogram
    terminates.

14
Local Variables- static vs. stack dynamic
  • Static
  • Direct access efficient
  • No runtime overhead to allocate/deallocate
  • History sensitive
  • Cannot support recursion
  • Stack Dynamic
  • Indirect addressing inefficient
  • Runtime overhead to allocate and deallocate
  • History insensitive
  • Supports recursion

15
Functions
  • Structurally resembles a program
  • Pure model
  • Produces and returns a value when executed has
    no side effects i.e., does not modify any of
    its parameters nor any variable defined outside
    the function.

16
Parameter Passing Methods (1)
  • Ways in which parameters are transmitted to and/
    or from subprograms
  • Three semantic models of parameter passing
  • in mode
  • Data is transmitted from the actual parameter to
    the corresponding formal parameter
  • Implementation model
  • Pass-by-value
  • out mode
  • Data is transmitted from the formal parameter to
    the corresponding actual parameter
  • Implementation model
  • Pass-by-result
  • in out mode
  • Data is transmitted from and to the actual
    parameter to and from the formal parameter
  • Implementation model
  • Pass-by-value-result
  • Pass-by-reference
  • Pass-by-name

17
Parameter Passing Methods (2)
caller
callee
( sub (a, b, c ) )
( void sub (int x, int y, int z ) )
Call
a
x
In mode
Return
b
y
Out mode
Call
z
c
Return
Inout mode
18
Pass-by-Value
  • When the subprogram execution starts the value of
    the actual parameter is copied to the stack to
    initialize the corresponding formal parameter.
    Any changes to the formal parameter is restricted
    within the subroutine.
  • The value of the actual parameter remains
    unchanged.
  • C example
  • void func( int p2 )

19
Pass-by-Result
  • Just before the subprogram execution terminates,
    the value of the formal parameter is copied to
    the stack and the corresponding actual parameter
    retrieves this value from the stack.
  • Any changes to the formal parameter during the
    subprogram execution is visible to the actual
    parameter.
  • Ada example
  • procedure adder( B out Float )

20
Pass-by-Value-Result
  • Combination of pass-by-value and pass-by-result
  • The value of the actual parameter is used to
    initialize the formal parameter and the value of
    the formal parameter is visible to the calling
    program.
  • Ada example
  • procedure adder( A in out Integer )

21
Pass-by-Reference
  • The subprogram is given the address of the actual
    parameter thus sharing the actual parameter
    between the calling module and the subprogram.
    Any changes to the parameter in the subprogram is
    visible to the calling program.
  • C example
  • void func1 (int p3 ) // implicitly dereferenced
  • Void func2 ( int p4 )
  • C example
  • void sumer( ref int oldSum )

22
Pass-by-Name
  • Textually substitutes the actual parameter in all
    occurrences of the formal parameter in the
    subprogram
  • Maybe implemented as pass-by-reference,
    pass-by-value or entirely different that any of
    the implementations discussed.

23
Pass-by-Name Example
  • procedure BigSub
  • integer global
  • integer array List 12
  • procedure sub (Param) // Param List global
    gt List 1
  • begin
  • Param 3 // List 1 3
  • global global 1 // global 1 1 2
  • Param 5 // List global gt List 2
    5
  • end
  • begin
  • List1 2
  • List2 2
  • global 1
  • sub ( List global ) // when sub terminates,
    List1 3, List2 5
  • End
Write a Comment
User Comments (0)
About PowerShow.com