Page 128 - Standard Handbook Of Petroleum & Natural Gas Engineering
P. 128
Computer Applications 113
Subprograms
The division of a program into a main program unit and one or more
subprogram units allows logical organization of the program into sections of
related operations and facilitates the coding, debugging, and replacement of
units of the program. Data are passed from one unit (or module) to another
through parameters (arguments) and/or through shared memory locations.
There are two types of subprograms usually available:
1. Function subprograms-Return a single value as the value of the function
name; these may be either extrinsic (user-defined) or intrinsic (provided
as part of the system library).
2. Subroutine or procedure subprograms-Return values through parameters or
global variables (see scope below).
The scope of a data item in a program determines which program units may
access (and change) the value of that data item. Global data may be accessed by
all program units, whereas local data are visible only to the unit(s) in which
they are defined. The method by which scope is determined depends on the
type of language being used. In FORTRAN, a COMMON block defined by a
COMMON statement in the main program allows data in that block to be
accessed by any module in which the block is defined. Parameters allow two
units to share data values; some types of parameters allow the passing of only
the value and not the location of the item, so that the subunit can read, but
not change, the value. In block structured languages, such as Pascal, the
structure determines the scope of the variables; the scope of a variable is the
block in which it was defined and all blocks contained therein. A variable in
this case is global to a sub-unit if it was declared not in that sub-unit but in a
higher-level unit which contains the sub-unit, e.g., all variables declared in the
main program are global to all program units.
Recursion is available in languages having dynamic memory allocation such as
ALGOL and Pascal. (Many versions, especially older ones, of FORTRAN and BASIC
have static memory allocation and do not permit recursion.) Direct recursion occurs
when a program unit calls itself; indirect recursion occurs when a chain of subpro-
gram calls results in the original calling unit being called again without returning
to a higher-level unit, e.g., MAIN + SUBA + SUBB + SUBC -+ SUBA, where
MAIN
I I I I
ABCD
General Programming Principles
Two characteristics of well-designed programs are
1. Generality-To as great an extent as possible for a particular problem, a
program should be able to operate on a wide variety of data sets with a
minimum of program revision, and the necessary changes should be as
simple to make as possible.
2. Portability-A program should adhere as closely as possible to a standard
version of a language and avoid highly machine-dependent constants and
constructions. Unidentified machine-dependent information should be
localized and identified for simplification of transport.

