Subprograms
Subprograms
Subprograms
• Programming languages provide two
fundamental abstraction mechanisms:
1.Process Abstraction → Encapsulating behavior
(functions, procedures, methods).
2.Data Abstraction → Encapsulating data and its
operations (objects, classes, ADTs).
Subprograms
• The first programmable computer, Babbage’s
Analytical Engine, built in the 1840s, had the
capability of reusing collections of instruction
cards at several different places in a program.
• In a modern programming language, such a
collection of statements is written as a
subprogram. This reuse results in savings in
memory space and coding time.
Subprograms
• A subprogram (also called a procedure,
function, method, or routine) is a self-
contained sequence of instructions that
performs a specific task within a program.
• Are the types of the actual parameters checked against the types of the formal parameters?
• If subprograms can be passed as parameters and subprograms can be nested, what is the referencing environment
of a passed subprogram?
• Pass-by-name - complex
Pass by result
Example
Example
Pass by name
Implementing Parameter-Passing Methods
Parameters That Are Subprograms
• The environment of the call
statement that enacts the passed
subprogram (shallow binding)
– Sub2 from sub4 => 4