Sub Programs
Sub Programs
• Introduction
• Fundamentals of Subprograms
• Design Issues for Subprograms
• Local Referencing Environments
• Parameter-Passing Methods
• Parameters That Are Subprogram Names
• Overloaded Subprograms
• Generic Subprograms
• Design Issues for Functions
• User-Defined Overloaded Operators
• Coroutines
Introduction
Two fundamental abstraction facilities
• Process abstraction
– Emphasized from early days
• Data abstraction
– Emphasized beginning in the 1980s
Fundamentals of Subprograms
• Each subprogram has a single entry point
• Keyword
– The name of the formal parameter to
which an actual parameter is to be bound
is specified with the actual parameter
– Parameters can appear in any order
Formal Parameter Default
Values
• In certain languages (e.g., C++, Ada),
formal parameters can have default values
(if not actual parameter is passed)
– Pass-by-value
– Pass-by-result
– Pass-by-value-result
– Pass-by-reference
– Pass-by-name
Models of Parameter Passing
Pass-by-Value
(In Mode)
• The value of the actual parameter is used to
initialize the corresponding formal parameter
– Normally implemented by copying
– Can be implemented by transmitting an
access path but not recommended
(enforcing write protection is not easy)
– When copies are used, additional storage
is required
– Storage and copy operations can be costly
Pass-by-Result
(Out Mode)
• When a parameter is passed by result, no
value is transmitted to the subprogram; the
corresponding formal parameter acts as a
local variable; its value is transmitted to
caller’s actual parameter when control is
returned to the caller
– Require extra storage location and copy
operation
• Potential problem: sub(p1, p1); whichever
formal parameter is copied back will
represent the current value of p1
Pass-by-Value-Result
(Inout Mode)
• A combination of pass-by-value and pass-
by-result
• Sometimes called pass-by-copy
• Formal parameters have local storage
• Disadvantages:
– Those of pass-by-result
– Those of pass-by-value
Pass-by-Reference
(Inout Mode)
• Pass an access path
• Also called pass-by-sharing
• Passing process is efficient (no copying and
no duplicated storage)
• Disadvantages
– Slower accesses (compared to pass-by-
value) to formal parameters
– Potentials for un-wanted side effects
– Un-wanted aliases (access broadened)
Pass-by-Name
(Inout Mode)
• By textual substitution
Disadvantages
-value In
-result Out
-name ?
Implementing Parameter-Passing
Methods
• In most language parameter communication
takes place thru the run-time stack
• C
– Pass-by-value
– Pass-by-reference is achieved by using
pointers as parameters
• C++
– A special pointer type called reference
type for pass-by-reference
Parameter Passing Methods of
Major Languages
• Java
– All parameters are passed are passed by
value
– Object parameters are passed by
reference
• C#
– Default method: pass-by-value
– Pass-by-reference is specified by
preceding both a formal parameter and its
actual parameter with ref
Parameter Passing Methods of
Major Languages (continued)
• Ada
– Three semantics modes of parameter
transmission: in, out, in out; in is the
default mode
– Formal parameters declared out can be
assigned but not referenced; those
declared in can be referenced but not
assigned; in out parameters can be
referenced and assigned
• Design Issues: