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
What are the 3 semantic models for passed parameters
in mode
out mode
inout mode
What is an “in mode” parameter
receives data from the corresponding actual parameter
What is an “out mode” parameter
transmits data to the corresponding actual parameter
What is an “inout mode” parameter
receives data from the corresponding actual parameter and transmits data to the corresponding actual parameter
What are the two conceptual models for how data is transferred by parameter
the value is copied (to the caller, to the called, or both ways)
an access path is transmitted (typically in the form of a pointer of reference)
The 4 implementation models for parameter passing
pass-by-value
pass-by-result
pass by value-result
pass by reference
2 characteristic of the pass-by-value model
in mode
normally implemented by copy
2 ways to implement value copying for parameter transferring
a physical move (the value moved from one location to another using MOV)
an access path (the value is fetched by getting value located at access path)
2 Disadvantages of both parameter copying schemes with pass-by-value
access path method:
1) must write protect in the called subprogram
2) accesses via indirect addressing cost more
physical move:
1) requires more storage
2) cost of moves
Main advantage of pass-by-value
very fast for scalars in both linkage cost and access time
3 characteristic of the pass-by-result model
out mode
local value is passed back to the caller
a physical move used
3 characteristics of pass by value-result
inout mode
AKA pass-by-copy
uses physical move both ways
2 characteristics of pass-by-reference
transmits an access path to (typically an address) the subprogram
the subprogram is able to access the actual parameter in calling program unit
1 advantage and 2 disadvantages to pass-by-reference
Advantage:
pass process is efficient
disadvantage:
- slower accesses
- can allow aliasing
What structure is used to implement parameter passing?
run-time stack (for both value and reference)
definition of pass-by-assignment
when the actual parameter value is assigned to the formal parameter. The actual value though remains unchanged
used in python and ruby
Biggest design consideration for parameter passing
one-way vs two-way parameters
one-way parameters => support Good programming practices of limited access to variables
two-way parameters => support efficiency because passing by reference is a much faster way to pass large structures
definition of an overloaded subprogram
one that has the same name as another subprogram in the same referencing environment
each version of an overloaded subprogram must have a unique protocol
How is the meaning of a call to an overloaded subprogram determined
by the parameter list
Difference between templates/generic functions in C++ and Java
in C++ generic parameters can be primitive types. Java they cannot!
in C++, the compiler creates an explicit version of the generic function for every different set of parameters passed to it. Java creates one “raw” function of all Object types and then casts these Object types for each different set of parameters passed
Java allows restrictions on the range of classes that can be parameters. C++ does not
independent compilation
the compilation of some units of the program separately from the rest of the program without the benefit of interface information
separate compilation
compilation 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
2 PLs using independent compilation
FORTRAN II
FORTRAN 77
4 PLs using separate compilation
Ada
Modula 2
C++
Java
3 design issues with functions
are side effects allowed
what type of values can be returned
how many values can be returned
What return types ARE NOT allowed in C, C++
arrays
functions
4 PLs which allow return values of ANY type
Ada
Python
Ruby
Lua
3 PLs that allow functions to return MORE than 1 value
Ruby
Lua
F#
definition of coroutine
a subprogram that has multiple entry points and controls them itself
3 characteristics of coroutines
a call to a coroutine is named resume
they provide quasi-concurrent execution because their execution is interleaved and not overlapping
typically coroutines repeatedly resume each other
how coroutines work
make the first call to resume which starts coroutine from its beginning (method A)
method A executes until it reaches a resume B statement
then the state of method A is saved and method B begins execution
B executes until it reaches a resume A statement
method A then executes from where it previously left off when it had called resume B.
A executes until another resume B statement
B then continues its execution from where it left off when it called resume A
[this process repeats, possibly indefinitely]