Chapter 9 - Subroutines Flashcards
3 characteristics of subprograms
1) have a single entry point
2) the caller is suspended during the execution of the called subprogram
3) control always returns to the caller when the called subprogram’s execution terminates
9 design issues with subprograms
1) what parameters passing methods are provided
2) are parameters typed checked
3) are local variables static or dynamic
4) what is the referencing environment of a passed subprogram
5) are parameter types in passed subprograms checked
6) can subprograms be overloaded
7) can subprogram definitions be nested
8) are subprograms allowed to be generic
9) is separate or independent compilation supported
definition of “subprogram definition”
a description of the actions of a subprogram abstraction
definition of “subprogram call”
an explicit request that the subprogram be executed
definition of “subprogram header”
the first line of the definition, including the name, kind of subprogram, and the formal parameters
definition of “parameter profile” of a subprogram
the number, order, and types of the subprogram’s parameters
definition of “protocol” of a subprogram
a subprogram’s parameter profile plus its return type (if it is a function)
definition of “active subprogram”
if a subprogram after being called has begun its execution but has not completed it
3 purposes of subroutine headers
indicate the syntax that follows is a subroutine
names the subroutine
enumerates the subroutine’s parameters
ex: “def” in Python/Ruby
definition of “subprogram declaration”
provides the protocol of a subprogram but not the body
what is formal parameter vs actual parameter
formal parameter: a dummy variable listed in the subprogram header and used in the body of the subprogram
actual parameter: a value or address used in the subprogram call statement
ex: def f(x): [Here ‘x’ is a formal parameter]
a = 5
f(a) [Here ‘a’ is an actual parameter]
definition of “parameterized computation”
a subprogram with parameter access to the data that it is to process
definition of positional parameters
are actual parameters that are correlated with formal parameters by their position in the subprogram header.
def f(x, y, z): ... f (1, 2, 3) [x=1, y=2, z=3]
definition of keyword parameters
actual parameters that are matched to formal parameters by keyword. That is the pairing of formal parameter names to actual parameter values
def sort(list, length) ... sort(list=a, length=n)
The main disadvantage to using keyword parameters?
the user of the subprogram must know the names of the formal parameters
How to do subprogram calls with a mix of keyword and positional parameters
The positional parameters are placed in their correct positions, and any parameters listed after the first keyword parameter must also be keyword parameters
definition of default parameters
a default value that is used by the subprogram if no actual parameter is passed to the formal parameter in the subprogram header
Default parameter handling in Python vs C++
in python, all actual parameters after the absent one must be keyworded
ex: def f(x, y=2, z) ==> f(1, z=3) [z must be keyworded]
in C++, default parameters must be the last formal parameters in the subprogram header. Any formal parameter after the default parameter must also be default parameters
ex: int f (x, z, y=2) ==> f(1,3) [y is last parameter in header]
4 PLs that support procedures
Pascal
Ada
Modula2
FORTRAN
What does it mean to “fake” a procedure
in PLs that do not support procedures (but only support functions) the use of a “void” or “null” return value essentially mimics the behavior of a procedure