Flow of control between programs and subprograms

There are a number of possible flows between COBOL main programs and subprograms.

A run unit is a running set of one or more programs that communicate with each other by COBOL static or dynamic CALL statements. In a CICS® environment, a run unit is entered at the start of a CICS task, or invoked by a LINK or XCTL command. A run unit can be defined as the execution of a program defined by a PROGRAM resource definition, even though for dynamic CALL, the subsequent PROGRAM definition is needed for the called program. When control is passed by a XCTL command, the program receiving control cannot return control to the calling program by a RETURN command or a GOBACK statement, and is therefore not a subprogram.

Each LINK command creates a new CICS application logical level, the called program being at a level one lower than the level of the calling program (CICS is taken to be at level 0). Figure 1 shows logical levels and the effect of RETURN commands and CALL statements in linked and called programs.

Figure 1. Flow of control between COBOL programs, run units, and CICS
Flow of control between COBOL programs and subprograms, described in preceding and following text.

A main, or level 1 program can use the COBOL GOBACK or STOP RUN statements, or the CICS RETURN command to terminate and return to CICS. It can use a COBOL CALL statement to call a subprogram at the same logical level (level 1), or a CICS LINK command to call a subprogram at a lower logical level. A called subprogram at level 1 can return to the caller using the COBOL GOBACK statement, or can terminate and return to CICS using EXEC CICS RETURN.

A subprogram executing at level 2 can use the COBOL GOBACK or STOP RUN statements, or the CICS RETURN command to terminate and return to the level 1 calling program. It can use a COBOL CALL statement or a CICS XCTL command to call a subprogram at the same level (level 2). A subprogram called using the COBOL CALL at level 2 can return to the caller ( at level 2) using the COBOL GOBACK statement, or can return to the level 1 calling program using EXEC CICS RETURN. A subprogram called using XCTL at level 2 can only return to the level 1 calling program, using GOBACK, STOP RUN or EXEC CICS RETURN.

See Application program logical levels for more information about program logical levels.