Chapter 9: Subprograms
9.4 Local Referencing Environments
Local Variables
Static
Require no run-time overhead for allocation and deallocation
Stack Dynamic
Bound to storage at start of execution(of subprogram) and unbound when execution terminates
Nested Subprograms
created to be able to create a hierarchy of both logic and scopes
9.5 Parameter-Passing Methods
Semantics
In mode
Receive data from the corresponding actual parameter
Out mode
Transmit data to actual parameter
Inout mode
Receive and transmit data from and to the parameter
Models
Pass-by-Value
Value of actual parameter is used to initialize the corresponding former parameter
Pass-by-Result
(For out-mode parameters) No value is transmitted to the subprogram
Pass-by-Value-Result
(For inout-mode parameters) Actual values are copied
Combination between pass-by-value and pass-by-result
Pass-by-Reference
transmits an access path (an address) to the called subprogram
Pass-by-Name
The actual parameter is textually substituted for the corresponding formal parameter in all its occurrences in the subprogram
Type Checking Parameters
Types of actual parameters checked for consistency w/ the types of corresponding formal parameters
Design Considerations
Efficiency
9.6 Parameters That Are Subprograms
Complications
Type checking the parameters of the activations of the subprograms that were passed as parameters
Which referencing environment should be used
Shallow Binding
The environment of the call statement that enacts the passed subprogram
Deep Binding
The environment of the definition of the passed subprogram
Ad Hoc Binding
The environment of the call statement that passed the subprogram as an actual parameter
9.9 Overloaded Subprograms
Subprogram has same name as another but has a least one difference in:
Number of parameters
Order of parameters
Types of parameters
Return type
Can be found in
C++
Java
C#
9.10 Generic Subprograms
Polymorphic
Can take parameters of different types on seperate calls
Subtype polymorphism
The type of an object can be derived from T
Parametric polymorphism
Able to use different parameters per call instance
Generic in C++
Template function
Class Identifier
Typename Identifier
Generic in Java 5.0
Uses
Collection<?>
is a wildcard type
Generic in C#
No support for Wildcard type
actual type can be omitted if compiler can infer
Generic in F#
Automatic generalization
If no type info included, assume generic
9.3 Design Issues for Subprograms
Which type of Subprogram
Overloaded Subprograms
Same name for
multiple subprograms
Generic Subprograms
Subprogram which can be
used with any input type
Closure
Nested Subprogram
9.2 Fundamentals of Subprograms
Characteristics
Single Entry Point
Calling program suspended until end
Returns to calling program at end
Definitions
Subprogram Definitions
Describes interface
Subprogram call
Request subprogram execution
Considered active during call
Subprogram Header
Chooses call name
Names parameters
Specifies subprogram variety
Specifies return value
Parameters
Formal parameters
Ones in function call,
exist only while the
subprogram is called
Actual Parameters
Parameters initialized within subprogram
Positional Parameters
Actual parameters which take
the values of their corresponding
formal parameter in order
Keyword Parameter
Formal Parameter attached
to Actual Parameter in Subprogram call
Procedures and Functions
Procedures
Can change outside parameters
Functions
Mathematic equation
9.1 Introduction
Two fundamental abstractions
Process abstraction
First programmable computer used
via reusable cards
Modern versions are written as subprograms
Not the same as methods.
Data abstraction
9.8 Design Issues for Functions
Functional Side Effects
In-mode only
No side effects from parameters or aliasing of parameters/globals
Pass-by-value/pass-by-reference
Functions can possibly cause side effects and aliasing
Types of Returned Values
No arrays/functions
C
Any type
Ada
Python
Ruby
Subprograms as objects
Ruby
Python
Number of Returned values
1 Return
Most languages
Multiple Returns
Ruby
Tuples
ML
Python
F#