Subprograms - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Subprograms

Description:

A subprogram definition describes the interface to and the actions of the ... Sumer (Length = My_Length, List = My_Array, Sum = My_Sum) ... – PowerPoint PPT presentation

Number of Views:161
Avg rating:3.0/5.0
Slides: 43
Provided by: jhy9
Category:

less

Transcript and Presenter's Notes

Title: Subprograms


1
Subprograms
  • Jianhua Yang
  • Department of Math and Computer Science
  • Bennett College

2
Outline
  • Fundamentals of Subprograms
  • Design issues
  • Local Referencing Environments
  • Parameter-Passing Methods
  • Parameters that are subprogram names
  • Overloaded Subprograms
  • Generic Subprograms
  • Design Issues for functions
  • User-defined overloaded Operators
  • Coroutines

3
9.1 Fundamentals of Subprograms
  • Characteristics
  • Single entry point
  • Caller suspended
  • Return to caller

4
1. Basic Definition
  • A subprogram definition describes the interface
    to and the actions of the subprogram abstraction.
  • Procedure
  • Function

5
Subprogram header
  • 1. it is a subprogram with a type
  • 2. specifying its name
  • 3. specifying a list of parameters

Fortran Subroutine Adder (parameters) Ada
procedure Adder (parameters) C-base
language void adder (parameters)
6
Parameter profile
  • It contains the number, order, and types of its
    formal parameters.

7
2. Parameters
  • Formal parameters
  • Actual parameters
  • Positional parameters
  • Keyword parameters

Sumer (Length gt My_Length, List gt
My_Array, Sum gt My_Sum)
8
Default value for formal parameters
  • Ada function header
  • function Compute_Pay (IncomeFloat
  • Exemptions Integer 1
  • Tax_Rate Float) return Float

Call this function Pay Compute_Pay(2000.0,Tax_
Rategt0.15)
9
C example
  • float compute_pay (float income, float tax_rate,
    int exemptions 1)

The default parameters must appear last.
Call this function pay compute_pay(2000.0,
0.15)
10
3. Procedures and Functions
  • They are two distinct categories of subprograms

11
Procedure
  • Procedures are collections of statements that
    define parameterized computations.

12
Interaction between procedure and its caller
  • 1. Visible variables in both a procedure and the
    caller can be changed by the procedure.
  • 2. if a procedure has formal parameters that
    allow the transfer of data to the caller, those
    parameters can be changed.

13
Function
  • Different from a procedure, it modifies
  • Neither its parameters
  • Nor any variables defined outside the function.
  • Functions are called by appearances of their
    names in expressions, along with the required
    actual parameters.
  • Return a value to the calling code (caller).

14
Function example in C
  • float power (float base, float exp)
  • Called with
  • result 3.4 power(10.0, 3)

Functions can be used as a procedure
15
9.2 Design Issues for Subprograms
  • Parameter-passing method
  • Type check
  • Local variables allocation statically or
    dynamically
  • Nested definition
  • Subprogram as parameter
  • Overload
  • Generic.

16
9.3 Local referencing environments
  • Variables that are defined inside subprograms are
    called local variables
  • The scope is usually the body of the subprogram
    in which they are defined.

17
Local variables
  • Static
  • Stack-dynamic

They are bound to storage when the subprogram
begins execution and are unbound from storage
when that execution terminates.
18
Stack-dynamic
  • Ad.
  • Flexibility
  • Space share
  • Da.
  • Time cost
  • Indirect access
  • Non history-sensitive

19
Static
  • Ad.
  • Efficiency
  • No run-time overhead
  • Da.
  • Inability to support recursion
  • Space share problem

20
Example
  • int adder (int list, int listlen)
  • static int sum0
  • int count
  • for(count0 count lt listlen count)
  • sum listcount
  • return sum

21
9.4 Parameter-Passing Methods
  • Parameter-passing methods are the ways in which
    parameters are transmitted to and/or from called
    subprograms.

22
1. Semantics Models of Parameter Passing
  • In mode
  • Out mode
  • In-out mode

23
2. Implementation models of parameter passing
  • Pass-by-value
  • Pass-by-result
  • Pass-by-value-result
  • Pass-by-reference
  • Pass-by-name

24
1) pass-by-value
Callee Void sub (int x, int y, int z)
Caller sub (a, b, c)

In mode

Out mode


Inout mode
25
Disadvantages
  • 1. space cost
  • 2. large data transfer cost too much time

26
2) pass-by-result
  • It is an implementation model for
    out-mode-parameters.
  • The same disadvantages as 1).

27
3) Pass-by-value-result
  • This one is in effect a combination of
    pass-by-value and pass-by-result.
  • Also called pass-by-copy.
  • The same disadvantages as the previous ones.

28
4) Pass-by-reference
  • Rather than copying data values back and forth,
    this method transmits an access path, usually
    just an address, to the called subprogram.
  • Its advantage is its efficiency in terms of time
    and space.
  • Its disadvantages
  • 1. indirect accessing
  • 2. inadvertent errors
  • 3. aliases problem

29
Example
void sub(int param)
  • void main()
  • int global
  • sub(global)

Memory block
30
5)pass-by-name
  • skip

31
3. Multidimensional Arrays as Parameters
  • void fun (int matrix10)
  • Void main()
  • int mat510
  • fun (mat)

32
Examples of parameter passing
void swap1( int a, int b) int
tempa ab btemp
void swap2( int a, int b) int
tempa ab btemp
swap2(c, d)
swap1(c, d)
33
9.5 Parameters that are subprogram names
  • In C, and C, function name can not be used as a
    parameter
  • But we can use a pointer point to a function.

34
9.6 Overloaded Subprograms
  • Overloaded operator is one that has multiple
    meanings.
  • Overloaded subprogram is a subprogram that has
    the same name as another subprogram in the same
    referencing environment.
  • The meaning of a call to an overloaded subprogram
    is determined by the actual parameter list.

35
example
  • void fun (int, int)
  • void fun (float)
  • void fun()

36
9.7 Generic subprograms
  • Purpose is to increase the reusability of
    software
  • A generic or polymorphism takes parameters of
    different types on different activations.

37
1. Generic Functions in C
  • Generic functions in C have the descriptive
    name of template functions.
  • General form
  • template lttemplate parametersgt
  • A template parameter has one of the following
    forms
  • class identifier
  • typename identifier

38
Example
  • template ltclass Typegt
  • Type max (Type first, Type second)
  • return first gt second ? First second

int max (int first, int second) return first gt
second ? first second
39
Generic sort example
template ltclass Typegt void generic_sort ( Type
list, int len) int top, bottom Type
temp for (top0 topltlen-2 top) for
(bottom top 1 bottom ltlen -1 bottom) if
(listtop gt listbottom) temp
listtop listtop listbottom listbo
ttom temp
float flt_list100 generic_sort(flt_list,
100)
40
9.8 Coroutines
  • A special kind of subprogram
  • Might have multiple entry-points
  • The invocation of a coroutine is called a resume
    rather than a call
  • Only one coroutine actually executes at a given
    time
  • Quasi-concurrency

41
example
A
B
resume B resume B resume B
resume A resume A
42
Summary
  • Subprogram definitions
  • Local referencing environments
  • Parameter passing
  • Overloaded subprograms
  • Generic subprograms
  • Coroutines.
Write a Comment
User Comments (0)
About PowerShow.com