Structuring a COBOL Program

COBOL provides access to procedure-like subdivisions within the program, as well as to procedures outside the program.

Internal Procedure Structure

Paragraphs and sections within a COBOL program are not considered blocks, because executing a paragraph or a section does not result in the creation of an activation record. Paragraphs and sections therefore do not affect the definition of critical blocks.

If the Binder is used to bind a procedure from a separate object code file into the program, then the bound-in procedure is considered a separate block. The bound-in procedure could be another COBOL program or a procedure from a program written in some other language. A COBOL program can enter, but cannot initiate, a bound-in procedure.

COBOL85 provides a structure called nested programs that is not available in COBOL74. Nested programs are programs that reside inside another program or inside another nested program. Nested programs resemble ALGOL procedures in the respect that nested programs can include declarations of local variables. However, the rules determining the scope of variable declarations in COBOL85 differ from the scope rules in ALGOL. The COBOL85 scope rules are explained in the interprogram communication discussion in the COBOL ANSI-85 Programming Reference Manual, Volume 1: Basic Implementation.

COBOL85 can enter, but cannot initiate, nested programs.

Note: COBOL85 is currently implemented in such a way that exiting a nested program cannot cause a CRITICAL BLOCK EXIT error. However, this implementation is subject to change. Nested programs may affect the critical block definition in future releases.

Another structure unique to COBOL85 is that of consecutive programs. Consecutive programs are completely separate programs that are stored, one after the other, in the same source file. When you compile a source file that contains consecutive programs, the compiler creates a separate object code file for each consecutive program. The resulting object code files are not linked in any way and have no special abilities related to tasking or interprocess communication.

External Procedure Structure

The following rules govern COBOL access to external procedures:

  • Separate programs

    A COBOL program can declare external procedures and use them to initiate separate programs.

  • Passed external procedures

    COBOL does not provide any method for passing procedures as parameters. Therefore, a COBOL program generally has no access to passed external procedures.

    One exception to this rule occurs when a program passes a constant or an expression by name to a COBOL program. The system creates a procedure called a thunk, whose purpose is to evaluate the constant or expression. Whenever the COBOL program interrogates the parameter, the system executes the thunk on the COBOL program's process stack.

  • Imported library procedures

    A COBOL program can enter, but cannot initiate, a procedure imported from a library.

    A critical block exit error can occur if the COBOL program terminates before an asynchronous offspring or a coroutine. For information about how to prevent such critical block exits, refer to Understanding Interprocess Relationships.