Software II: Principles of Programming Languages: Lecture 9 - Subprograms
Software II: Principles of Programming Languages: Lecture 9 - Subprograms
Programming Languages
Lecture 9 – Subprograms
Fundamentals of Subprograms
• Each subprogram has a single entry point
• The calling program is suspended during
execution of the called subprogram
• Control always returns to the caller when
the called subprogram’s execution
terminates
Subprogram Definitions
• A subprogram definition describes the
interface to and the actions of the
subprogram abstraction
– In Python, function definitions are executable; in all
other languages, they are non-executable
– In Ruby, function definitions can appear either in or
outside of class definitions. If outside, they are methods
of Object. They can be called without an object, like a
function
– In Lua, all functions are anonymous
Basic Definitions
• A subprogram call is an explicit request that the
subprogram be executed
• A subprogram header is the first part of the
definition, including the name, the kind of
subprogram, and the formal parameters
• The parameter profile (aka signature) of a
subprogram is the number, order, and types of its
parameters
• The protocol is a subprogram’s parameter profile
and, if it is a function, its return type
Basic Definitions (continued)
• Function declarations in C and C++ are often
called prototypes
• A subprogram declaration 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
Actual/Formal Parameter
Correspondence
• Positional
– The binding of actual parameters to formal
parameters is by position: the first actual
parameter is bound to the first formal parameter
and so forth
– Safe and effective
Actual/Formal Parameter Correspondence
• Keyword
– The name of the formal parameter to which an
actual parameter is to be bound is specified
with the actual parameter
– Advantage: Parameters can appear in any order,
thereby avoiding parameter correspondence
errors
– Disadvantage: User must know the formal
parameter’s names
Ruby Blocks
• Ruby includes a number of iterator
functions, which are often used to process
the elements of arrays
• Iterators are implemented with blocks,
which can also be defined by applications
• Blocks are attached methods calls; they can
have parameters (in vertical bars); they are
executed when the method executes a
yield statement
Ruby Blocks – An Example
def fibonacci(last)
first, second = 1, 1
while first <= last
yield first
first, second = second, first + second
end
end
Implementing Parameter-Passing
Methods
• In most languages parameter
communication takes place thru the run-
time stack
• Pass-by-reference are the simplest to
implement; only an address is placed in the
stack
Implementing Parameter-Passing Methods
Multidimensional Arrays as
Parameters
• If a multidimensional array is passed to a
subprogram and the subprogram is
separately compiled, the compiler needs to
know the declared size of that array to build
the storage mapping function
Multidimensional Arrays as Parameters: C
and C++
• Similar to Ada
• Arrays are objects; they are all single-
dimensioned, but the elements can be arrays
• Each array inherits a named constant (length
in Java, Length in C#) that is set to the length
of the array when the array object is created
Design Considerations for Parameter Passing