COBOL Subprograms

Comparison with Paragraphs

It is assumed that the reader is already familiar with fact that, within the PROCEDURE DIVISION of a COBOL (sub)program, each paragraph is a unit of executable code that is "callable" ---via the PERFORM verb--- from within that (sub)program. In effect, all the paragraphs in (the PROCEDURE DIVISION of) a COBOL (sub)program are parameter-less "routines" having access to the same data, namely, all those data items declared in the DATA DIVISION of that (sub)program.

In contrast, a COBOL subprogram is a separate compilation unit; that is, it belongs in a file by itself and is compiled independently of its clients (i.e., other COBOL (sub)programs that call it). In order to produce an executable file, the object code files produced by the separate compilations of a main program and any subprograms it calls (directly or indirectly) must be linked. (You may wish to read instructions on how to compile, link, and execute COBOL programs in a VMS environment.)

Because the data declared in one compilation unit is not visible in any other (and hence, subprograms cannot communicate with their callers through the mechanism of shared data, as do the paragraphs within a compilation unit), subprograms communicate with their callers through the mechanism of argument-passing. (What are called "parameters" in some languages are referred to as "arguments" in others, including COBOL and FORTRAN.)

Comparision with Programs

The syntactic form of a COBOL subprogram is nearly identical to that of a COBOL program; in particular, it has the same four divisions: IDENTIFICATION, ENVIRONMENT, DATA, and PROCEDURE.

In a subprogram, however, the DATA DIVISION includes ---in addition to the FILE and WORKING-STORAGE sections--- a LINKAGE section, where the subprogram's formal arguments are described (via PICTURE clauses, etc.)

Another syntactic difference is that, in a subprogram, the PROCEDURE DIVISION header includes a USING clause that lists the names of the formal arguments and, in so doing, indicates the order in which the corresponding actual arguments must be listed by the caller in making a call to the subprogram.

Finally, to terminate execution of a subprogram (and return control to its caller), the statement EXIT PROGRAM is used, rather than STOP RUN. The latter statement will, as usual, have the effect of terminating execution of the whole program; in other words, if it is executed within a subprogram, control will not return to its caller.

Calling and Passing Arguments to a Subprogram

A statement used for calling a subprogram has the syntax

CALL <subprogram-name> USING <argument-list>

A subprogram's name is that which is specified in its PROGRAM-ID paragraph. The argument list is a sequence of data-names (or literals), each (optionally) preceded by one of the two phrases BY CONTENT or BY REFERENCE, which specifies the mode under which the argument is passed. The former is analogous to a non-VAR parameter in Pascal, or to an in parameter in Ada, or to all parameters in C and Java (which give you no choice but to "pass by value"). The latter is analogous to a VAR parameter in Pascal or to an in out parameter in Ada. The default is BY REFERENCE.

For example, the following invokes the subprogram Junk and passes to it four arguments, the first and last BY REFERENCE and the middle two BY CONTENT:

              CALL 'Junk' USING
                 BY REFERENCE Garbage
                 BY CONTENT   Refuse, 37
                 BY REFERENCE Rubbish 

For another example, note that the PROGRAM-ID of this subprogram is Plus-One; then note that, to call it, another (sub)program would (most likely) use a statement of the form


CALL 'Plus-One' USING <argument-list>

as is illustrated by this calling program. (The need for quotes surrounding the name brings up a rather interesting issue, which is discussed below.)

Miscellaneous Facts

Reference

For a much more complete description of subprograms (and nested programs) in COBOL, see Chapter 23 of Comprehensive COBOL.