“Habit a second nature? Habit is ten times nature!” — The Duke of Wellington
The world would be a pretty tedious (and dangerous) place if we did not learn: if we had to figure out how to do the same things, day after day. Imagine having to figure out, again, tying shoes every morning. Imagine having to renew the knowledge not to touch hot burners every time we used a stove. The same principle holds true in programming: it is a benefit to store our knowledge of how to do something in such a way that it can be reused.
A subprogram is a computer program that can be used in other computer programs — incorporated by reference, so to speak. For example, in the first exercise in this section, we program a square root subroutine. In the second exercise in this section, we use a computation that requires square roots. By constructing a subprogram — a square root “black box” — we can reuse the knowledge of how to extract square roots in the second exercise.
Two types of subprograms are generally recognized. These are called functions and subroutines, and differ in whether they return a value. Generally, a function returns a value, and a subroutine does not. More importantly, functions are also generally regarded as being free of side effects — they do not change the program’s state other than by computing their result. Subroutines, lacking a return value, must change the program’s state — its variables or stored data — or its environment — the world surrounding the program — to have any effect at all.
A subprogram that always computed the square root of four would have markedly limited utility, certainly much more limited utility than a subprogram that computed the square root of any non-negative number. But with a subprogram that computes the square root of any non-negative number, there arises the question of how to communicate which square root is desired. This is done through a parameter. A parameter is a value that is passed to a subprogram to serve as the basis of its computations. Our example square root subprogram would need a variable to tell it the value for which the square root is desired.
The mechanism of communication with a subprogram leads to a further distinction among subprograms. An open subprogram is one that locates its arguments and temporary variables in storage that is shared with other routines — variables in the calculator, for example, or global variables in JavaScript and Visual Basic for Applications. A closed subprogram is one that has special storage for its arguments and its internal variables, generally storage allocated specifically for the purpose of calling and executing the subprogram. Such storage exists only for the duration of the subprogram’s execution, and is not accessible outside of the subprogram itself.
(The terms “open” and “closed” are also used of subprograms that are coded in-line at what would be their calling site, or that are coded separately. This terminology is not further discussed here, but is used in other works.)
Generally, closed subprograms are preferred over open subprograms. This is because the remainder of the program must accommodate the internal structure of open subprograms. If the square root subprogram, for example, uses variables A, B, C, and D for its own purposes, then these variables must not be used elsewhere in the program — or at least their contents must not be expected to survive a call to the square root subprogram. Fortunately, the JavaScript and Visual Basic for Applications languages make it easy to construct closed subprograms. Unfortunately, the calculator makes it very difficult to construct closed subprograms. We shall, however, consider simulating closed subprograms with the calculator.
[ Previous page | Top of page | Next page ]
Copyright © 2001 Brian Hetrick
Page last updated 30 December 2001.
Tutorial
Building Blocks I
Control Flow II
Subprograms
Introduction
Basic I/O
Algorithms
A First Program
Answers
Modularization
Data Structures I
Recursion
Program Attributes
Building Blocks II
Algorithm Analysis
Structuring
Data Structures II
Abstract Types
Objects
Problem Analysis
Reference Card
![]()