COEN%20171%20-%20Subprograms - PowerPoint PPT Presentation

About This Presentation
Title:

COEN%20171%20-%20Subprograms

Description:

Abstractions are simplifying mechanisms that eliminate irrelevant information ... what to do about actuals that are expressions? ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 34
Provided by: RonDan8
Learn more at: https://www.cse.scu.edu
Category:

less

Transcript and Presenter's Notes

Title: COEN%20171%20-%20Subprograms


1
COEN 171 - Subprograms
  • Procedural abstractions
  • Subprogram characteristics
  • Design issues and terminology
  • Scope
  • Activation records and displays
  • Parameters
  • Generic subprograms
  • Separate and independent compilation
  • Special issues for functions
  • Overloading

2
Procedural Abstractions
  • Abstractions are simplifying mechanisms that
    eliminate irrelevant information
  • Two kinds of procedural abstractions
  • subprograms
  • exceptions and concurrency
  • Procedure abstracts a program unit into a
    statement
  • procedure invocation used anywhere a statement
    can
  • creates new statements
  • Function abstracts a program unit into an
    expression
  • creates new operators
  • Motivations
  • improve readability, writability, maintenance
  • separate concept and implementation
  • improve reusability

3
Subprogram Characteristics
  • Fundamental characteristics
  • each subprogram has a single entry point
  • calling program unit is suspended during
    execution of called subprogram
  • control always returns to the calling program
    unit when the called subprogram terminates

4
Subprogram Design Issues
  • What parameter passing methods are provided?
  • Are parameter types checked?
  • Are local variables static or dynamic?
  • What is the referencing environment of a passed
    subprogram?
  • Are parameter types in passed subprograms
    checked?
  • Can subprogram definitions be nested?
  • Can subprograms be overloaded?
  • Are subprograms allowed to be generic?
  • Is separate or independent compilation supported?

5
Terminology
  • Subprogram definition describes actions of
    subprogram
  • name
  • numbers and types of parameters
  • and possibly modes
  • type of return value for function
  • binds name, type, and definition (executable
    code) at compile time
  • procedure foo (a, b in out INTEGER) is ... end
    foo
  • Subprogram header is the first line of the
    definition
  • name, kind of subprogram, formal parameters
  • Subprogram call (or invocation) is explicit
    request for subprogram to be executed
  • usually just write name
  • FORTRAN (call), COBOL (perform), BASIC (gosub)
    require special words
  • invocation binds subprogram (code) to an
    activation record

6
Terminology (continued)
  • Subprogram is active if it has been invoked but
    hasnt completed execution
  • The parameter profile of a subprogram is the
    number, order, and types of its parameters
  • The protocol of a subprogram is its parameter
    profile plus, if it is a function, its return
    type
  • A subprogram declaration (or prototype) provides
    the protocol, but not the body, of the subprogram
  • A formal parameter is a dummy variable listed in
    the subprogram header and used in the subprogram
  • An actual parameter represents a value or address
    used in the subprogram call statement
  • Sometimes argument

7
Scope
  • Scope of an identifier is the range of statements
    in which the identifier is visible
  • visible means it can legally be referenced
  • local variables are defined in the program unit
  • non-local variables are defined in some other
    block
  • global variables are defined in the outermost
    block
  • procedures and functions scope variables in most
    languages
  • some (C, Ada, Algol 60 and 68) allow compound
    statements to scope identifiers
  • the reference environment is the set of all
    identifiers visible at a point in the program

8
Scope (continued)
  • Static scoping (or lexical scoping) is based on
    the textual nesting structure of the program
  • when an identifier is referenced, the compiler
    first searches local program unit for declaration
    of that identifier
  • if not found, move out to containing program unit
    and search there, etc.
  • called procedure executes in the environment of
    its definition
  • Dynamic scoping is based on the execution order
    of program units
  • when an identifier is referenced, first search
    currently executing program unit for declaration
    of that identifier
  • if not found, move to program unit that invoked
    the currently executing one, and search there
  • called procedure executes in the environment of
    its caller
  • means no type checking at compile time, no way to
    protect local variables from program units
    further down call chain

9
Scope (continued)
  • With static scoping, print true
  • With dynamic scoping, print false
  • A hole in scope is when a local identifier
    hides the declaration of an outer identifier
  • A in example
  • Ada allows explicitly referencing the hidden
    identifier by specifying procedure name
  • MAIN.A ...
  • Procedure and function names are part of
    surrounding block, not the block they define
  • usually, Q cant invoke P

procedure MAIN is A, B Boolean true
procedure Q is A Integer begin
put (B) end Q procedure P is
B Boolean false begin
Q end P begin P end MAIN
10
Scope and Lifetime
  • Scope of a variable
  • for static scoping, runs from declaration to end
    of program unit in which declared
  • for dynamic scoping, runs from beginning of
    execution of program unit in which declared to
    end of execution
  • less any holes where it is redefined
  • Lifetime of a variable is the time when a
    variable is allocated memory
  • beginning of execution of program unit where
    declared to end of execution
  • exceptions are
  • C static or Algol-60 own variables
  • languages like FORTRAN where all variables are
    allocated statically
  • Scope usually lt lifetime

11
Activation Records
  • Activation records (run-time stack frames) are
    needed for recursive subprograms
  • one AR for each active invocation of a subprogram
  • AR contains
  • return address
  • location in calling subprogram to continue
    execution when called subprogram finishes
  • static link
  • pointer to AR for statically containing program
    unit
  • beginning of stack frame
  • dynamic link
  • pointer to AR for program unit that invoked this
    one
  • end of stack frame
  • values for parameters
  • space for all locally declared variables
  • temporary variables
  • eg, intermediate values in expression when invoke
    function

12
Activation Records (continued)
A
program A var I, X integer procedure B
var I, K real procedure C
var I, L integer begin if
X gt 0 then B end C begin
X X - 1 C end B begin X 2
B end A
X 0
I ?
B
I ?
K ?
C
I ?
L ?
B
I ?
K ?
C
I ?
L ?
13
Activation Records (continued)
  • With this approach and static scoping, references
    to variables then become ordered pairs
  • number of levels to follow static chain
  • offset from beginning of that AR
  • For dynamic scoping, dont need static link
  • do need information about identifiers defined in
    each block
  • Maybe dont want to actually keep links in ARs
  • keep them outside in some kind of display
  • eg, stack with one entry for each level
  • provides faster access to each level

14
Displays
  • Example program skeleton

program MAIN_3 procedure BIGSUB
procedure SUB1 end SUB1
procedure SUB2 procedure
SUB3 end SUB3
end SUB2 end BIGSUB
end. MAIN_3
15
Displays (continued)
  • Before the call, we have
  • ARI for SUB2
  • ARI for BIGSUB 2
  • 1
  • ARI for MAIN_3 0
  • Stack Display
  • After the call, we have
  • ARI for SUB3
  • ARI for SUB2
  • 3
  • ARI for BIGSUB 2
  • 1
  • ARI for MAIN_3 0
  • Stack Display

16
Static Chain vs. Display
  • References to locals
  • not much difference
  • References to nonlocals
  • if it is one level away, they are equal
  • if it is farther away, the display is faster
  • display is better for time-critical code, because
    all non-local references cost the same
  • Procedure calls
  • for one or two levels of depth, static chain is
    faster
  • otherwise, the display is faster
  • Procedure returns
  • both have fixed time, but the static chain is
    slightly faster
  • Overall
  • static chain is better, unless the display can be
    kept in registers

17
Parameters
  • Mechanism to convey information between calling
    program unit and called program unit
  • Fundamental modes
  • in mode only sends values to subprogram
  • implies can be expression
  • out mode only sends values back to calling
    program
  • in-out mode communicates both directions
  • out and in-out must provide location
  • Associating actual and formal parameters
  • positional
  • in order, left to right
  • some languages allow too few or too many
    arguments (C, various command languages)
  • programmers responsibility to ensure this is
    meaningful

18
Parameters (continued)
  • Parameter association (continued)
  • named (or keyword)
  • specify parameter formal name and corresponding
    value
  • Start_Value gt 1.3
  • mixed
  • Ada allows both positional and named association
  • positional must come first
  • Parameter defaults
  • Ada and C allow formal parameters to specify
    default values to use if corresponding argument
    is omitted
  • in C, all formal parameters following first
    with default must have defaults
  • then positional assignment of actuals
  • in Ada, all actuals must use named association
    after first one skipped

19
Parameter Passing Models
  • Mechanisms for implementing connection between
    actual and formal parameters
  • Call-by-value (in mode)
  • value of actual is copied to location of formal,
    and then is treated as a local variable
  • lots of overhead for large arguments
  • Call-by-result (out mode)
  • formal acts as local variable, when subprogram
    ends value is copied to location of actual
  • similar overhead issues as call-by-value
  • possible problems caused by timing
  • SUB ( X, X ) result depends on order in which
    values copied back
  • Call-by-value-result (in-out mode)
  • combines the mechanisms above

20
Parameter Passing Methods (continued)
  • Call-by-reference (in-out mode)
  • pass pointer to location of actual all
    references one level indirect
  • saves copying time, but slower access
  • aliasing a problem
  • procedure FOO (X, Y integer)
  • begin
  • X 2
  • Put Y
  • end
  • A 10
  • FOO( A, A)
  • in FORTRAN IV, could change a constant value by
    passing as argument
  • what to do about actuals that are expressions?
  • if have pointers and call-by-value, can get same
    effect

21
Parameter Passing Methods (continued)
  • Call-by-name (in-out mode)
  • formals act as if text of actuals is substituted
    everywhere formal occurs
  • only in Algol-60 and Simula-67
  • very different behavior than any other mechanism
    unless arguments are
  • scalar variables gt call-by-reference
  • constant expressions gt call-by-value

procedure bigsub integer global integer
array list12 procedure sub(param)
integer param behaves like begin
param 3 global global1 listglobal
3 global global1 param 5
end listglobal 5 begin list1
2 list2 2 global 1
sub(listglobal) end
22
Parameter Passing Methods (continued)
  • Can do very clever tricks but can also cause
    problems

Dijkstra, 1961 real procedure sum (add, indx,
len) value len real add integer indx, len
begin real temp temp 0 for
indx 1 step 1 until len do temp
temp add sum temp end then
sum(A, I, 100) is 100A for scalar A
sum(AI, I, 100) is sum of first 100 elts of A
for A a 1-dim array sum(AIAI, I, 100)
is sum of squares of first 100 elts of A
procedure swap(first, second) integer first,
second begin integer temp temp
first first second second temp
end consider swap (I, J) swap
(I, AI)
23
Language Choices of Parameter Mechanisms
  • FORTRAN
  • before 77, pass-by-reference
  • 77 - scalar variables are often passed by
    value-result
  • ALGOL 60
  • pass-by-name is default pass-by-value is
    optional
  • ALGOL W
  • pass-by-value-result
  • C
  • pass-by-value
  • Pascal and Modula-2
  • default is pass-by-value pass-by-reference is
    optional
  • C
  • like C, but also allows reference type
    parameters, which provide the efficiency of
    pass-by-reference with in-mode semantics

24
Language Choices of Parameter Mechanisms
(continued)
  • Ada
  • all three semantic modes are available
  • if out, it cannot be referenced
  • if in, it cannot be assigned
  • Java
  • like C, except only references
  • Type checking of parameters
  • now considered important for reliability
  • FORTRAN 77 and original C none
  • Pascal, Modula-2, FORTRAN 90, Java and Ada it is
    always required
  • ANSI C and C choice is made by the user

25
Procedures as Parameters
  • Want to do this for things like numerical
    integration or graphing routines
  • In strongly typed languages, must include header
    of procedures that can be legally passed as
    formal parameter
  • essentially, a procedure type
  • Actual argument is pointer to code for procedure
    and pointer (static link) to execution
    environment
  • called a closure
  • Then the question is, what is environment?
  • subprogram that calls passed procedure
  • shallow binding (SNOBOL4)
  • subprogram passed procedure declared in
  • deep binding

26
Procedures as Parameters (continued)
  • Consider this Modula 3 code

module MAIN ( exec environment for P
using deep binding ) type F procedure (X
integer) procedure P (X integer) begin ...
end P procedure Q (FP F) begin FP(5) end
Q ( exec environment for P and T using
shallow binding ) procedure R ()
( exec environment for T using deep binding )
procedure T (X integer) begin ... end T
begin ( R ) Q(P) Q(T)
end R begin R () end MAIN.
27
Generic Subprograms
  • A generic or polymorphic subprogram is one that
    takes parameters of different types on different
    activations
  • overloaded subprograms provide ad hoc
    polymorphism
  • A subprogram that takes a generic parameter that
    is used in a type expression that describes the
    type of the parameters of the subprogram provides
    parametric polymorphism
  • Ada
  • types, subscript ranges, constant values, etc.,
    can be generic in Ada subprograms and packages

28
Generic Subprograms (continued)
  • generic
  • type ELEMENT is private
  • type VECTOR is array (INTEGER range ltgt) of
    ELEMENT
  • procedure GENERIC_SORT (LIST in out VECTOR)
  • procedure GENERIC_SORT (LIST in out VECTOR) is
  • TEMP ELEMENT
  • begin
  • for INDEX_1 in LISTFIRST .. INDEXPRED
    (LISTLAST) loop
  • for INDEX_2 in INDEXSUCC (INDEX_1) ..
    LISTLAST loop
  • if LIST (INDEX_1) gt LIST (INDEX_2) then
  • TEMP LIST (INDEX_1)
  • LIST (INDEX_1) LIST (INDEX_2)
  • LIST (INDEX_2) TEMP
  • end if
  • end loop -- for INDEX_1 ...
  • end loop -- for INDEX_2 ...
  • end GENERIC_SORT
  • procedure INTEGER_SORT is new GENERIC_SORT (

29
Generic Subprograms (continued)
  • Ada generics are used to provide the
    functionality of parameters that are subprograms
    generic part is a subprogram

generic with function FUN (X FLOAT) return
FLOAT procedure INTEGRATE (LOWERBD in
FLOAT UPPERBD in
FLOAT RESULT out FLOAT)
is FUNVAL FLOAT begin ...
FUNVAL FUN (LOWERBD) ...
end INTEGRATE_FUN1 is new INTEGRATE (FUN gt
FUN1)
30
Generic Subprograms (continued)
  • C template functions are instantiated
    implicitly when the function is named in a call
    or when its address is taken with the operator

template ltclass Typegt void generic_sort (Type
list , int len) int top, bottom Type
temp for (top 0 top lt len - 2 top)
for (bottom top 1 bottom lt len - 1
bottom) if (list top gt list bottom)
temp list top list top
list bottom list bottom temp
// end of for (bottom ... float
flt_list 100 ... generic_sort (flt_list, 100)
31
Separate and Independent Compilation
  • Independent compilation is compilation of some of
    the units of a program separately from the rest
    of the program, without the benefit of interface
    information
  • Separate compilation is compilation of some of
    the units of a program separately from the rest
    of the program, using interface information to
    check the correctness of the interface between
    the two parts.
  • Language choices
  • FORTRAN II to FORTRAN 77 - independent
  • FORTRAN 90, Ada, Modula-2, C - separate
  • Pascal - allows neither

32
Functions
  • Can functions have side effects
  • changes other than returning a value
  • Ada says parameters cant change (only in mode
    parameters) other languages allow side effects
  • everyone allows access to non-local variables
  • What types can be returned by functions
  • FORTRAN scalar types
  • Pascal discrete types and pointers
  • C anything except arrays and functions
  • which are pointers
  • Ada any legal type
  • C C semantics and classes

33
Overloading
  • Procedures and functions are overloaded if there
    is more than one with the same name
  • Ada, Java, C allow if which one to use can be
    distinguished by type or number of arguments, or
    explicitly disambiguated
  • Ada and C allow user-defined overloaded
    operators
  • function (left, right sometype) return
    sometype is
  • begin ... end
  • then can use new as infix operator
  • declare A, B, C sometype
  • C A B
Write a Comment
User Comments (0)
About PowerShow.com