COBOL programs can contain other COBOL programs, thus creating a nested structure. This convenient technique, as well as keeping related programs in a modular framework, offers performance benefits for CALL statements between the programs.
When nesting programs, the nested program should be inserted immediately before the end program marker of the containing program.
Within a nested structure, you can call programs from the program in which they are contained: in the example below, blue1 can call blue2, and blue2 can call blue3. Recursive calls are not permitted.
It is also possible to call programs that specify the is common phrase in the program-id. Such common programs can be called from any program contained directly or indirectly within the same program that contains the common program.
The outer-most program in the nested structure (color in the example below) is the only program that may have a Configuration section within the nested structure.
The scope of names (data items, file connectors, etc...) within a nested structure are deemed to be local, in that they are accessible only to the program in which they are declared. To make those items available to other programs that are directly or indirectly nested, add the GLOBAL clause to the declaration. Named items that are subordinate to a global name are also considered global, and have the same scope.
Names throughout a nested structure do not have to be unique - that is, you can have the same named data item in more than one nested program - however, we recommend that names are unique, for ease of use, and to avoid referencing issues. Referenced names are searched using the following priority:
If no name is found, an error is generated. Note, the search purely searches for the referenced name; if the first name found is for an item not of the correct object type (such as a file connector found, instead of a data item), an error is generated.
Nested programs are compiled by specifying the outer-most program during compilation, which produces a single file of all nested programs.
In the following example, there are two programs directly nested within color: blue1 and green1. blue2 is directly nested within blue1, and is therefore indirectly nested in color, and so on.
green1 is marked as is common, and so blue2, which is nested indirectly in the color program (via blue1) can call it.
Identification Division. Program-id color. call "blue1". Identification Division. Program-id blue1. Procedure division. display "Displaying blue1". call "blue2". goback. Identification Division. Program-id blue2. Procedure division. display "Displaying blue2, directly nested in blue1". call "blue3". goback. Identification Division. Program-id blue3. Procedure division. display "Displaying blue3, directly nested in blue2" no advancing. display ", and indirectly nested in blue1". call "green1". display " from blue3.". goback. end program blue3. end program blue2. end program blue1. Identification Division. Program-id green1 is common. Procedure division. display "Displaying green1" no advancing. goback. end program green1. end program color.