Concepts of Programming Languages

(Sean Pound) #1

388 Chapter 9 Subprograms


S


ubprograms are the fundamental building blocks of programs and are there-
fore among the most important concepts in programming language design. We
now explore the design of subprograms, including parameter-passing meth-
ods, local referencing environments, overloaded subprograms, generic subprograms,
and the aliasing and problematic side effects that are associated with subprograms.
We also include discussions of indirectly called subprograms, closures, and coroutines.
Implementation methods for subprograms are discussed in Chapter 10.

9.1 Introduction


Two fundamental abstraction facilities can be included in a programming lan-
guage: process abstraction and data abstraction. In the early history of high-
level programming languages, only process abstraction was included. Process
abstraction, in the form of subprograms, has been a central concept in all
programming languages. In the 1980s, however, many people began to believe
that data abstraction was equally important. Data abstraction is discussed in
detail in Chapter 11.
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 several different kinds of savings, primarily memory space and coding time.
Such reuse is also an abstraction, for the details of the subprogram’s compu-
tation are replaced in a program by a statement that calls the subprogram.
Instead of describing how some computation is to be done in a program, that
description (the collection of statements in the subprogram) is enacted by
a call statement, effectively abstracting away the details. This increases the
readability of a program by emphasizing its logical structure while hiding the
low-level details.
The methods of object-oriented languages are closely related to the sub-
programs discussed in this chapter. The primary way methods differ from sub-
programs is the way they are called and their associations with classes and
objects. Although these special characteristics of methods are discussed in
Chapter 12, the features they share with subprograms, such as parameters and
local variables, are discussed in this chapter.

9.2 Fundamentals of Subprograms


9.2.1 General Subprogram Characteristics
All subprograms discussed in this chapter, except the coroutines described in
Section 9.13, have the following characteristics:


  • Each subprogram has a single entry point.

Free download pdf