Nested programs and ENTRY
Every page since the call lesson put two programs in one file, one after the other, each closed by END PROGRAM. That is the arrangement a source file can hold several times over, and it has two refinements: a program nested inside another, and a program with more than one entry point.
A nested program
A nested program is declared before the END PROGRAM of its parent, so it belongs to the parent. The compiler keeps it in the same module, and the call works by name like any other:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9(3) VALUE 40.
01 WS-R PIC 9(5) VALUE 0.
PROCEDURE DIVISION.
CALL "INNER" USING WS-A WS-R.
DISPLAY "outer " WS-A " " WS-R.
STOP RUN.
IDENTIFICATION DIVISION.
PROGRAM-ID. INNER.
DATA DIVISION.
LINKAGE SECTION.
01 L-A PIC 9(3).
01 L-R PIC 9(5).
PROCEDURE DIVISION USING L-A L-R.
COMPUTE L-R = L-A * 2
DISPLAY "inner " L-A " " L-R.
GOBACK.
END PROGRAM INNER.
END PROGRAM MAINP.
cobc -x -free -Wall -o nest nest.cob
./nest
inner 040 00080
outer 040 00080
The order of the two END PROGRAM lines is what makes INNER nested: the inner one closes INNER, the outer one closes MAINP. Write them the other way round and the file no longer parses, because the compiler is inside MAINP until the outer line.
ENTRY and a second name
ENTRY marks a second place where a call can start the same program. The program has one PROGRAM-ID and one set of data, and each entry point has its own parameter list:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9(3) VALUE 10.
01 WS-R PIC 9(5) VALUE 0.
PROCEDURE DIVISION.
CALL "CALC" USING WS-A WS-R.
DISPLAY "calc " WS-R.
CALL "TRIPLE" USING WS-A WS-R.
DISPLAY "triple " WS-R.
STOP RUN.
END PROGRAM MAINP.
IDENTIFICATION DIVISION.
PROGRAM-ID. CALC.
DATA DIVISION.
LINKAGE SECTION.
01 L-A PIC 9(3).
01 L-R PIC 9(5).
PROCEDURE DIVISION USING L-A L-R.
COMPUTE L-R = L-A * 2
GOBACK.
ENTRY "TRIPLE" USING L-A L-R.
COMPUTE L-R = L-A * 3
GOBACK.
END PROGRAM CALC.
cobc -x -free -Wall -o entry entry.cob
./entry
calc 00020
triple 00030
The first call used the name of the program and ran the statement after PROCEDURE DIVISION; the second entered at the ENTRY and ran from there. The data division is shared, so an item changed at one entry point is changed for the other.
One rule is worth remembering because it looks like a bug: the program is not callable by the name of the first paragraph or by any name but its PROGRAM-ID and its ENTRY list. A call to "DOUBLE" in this program stops the run with module not found, which is the error of the call lesson.
What a nested program cannot see
Nesting is containment, not sharing. The inner program has its own data division and does not see the working storage of its parent; a reference to a parent field is an undefined name:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9(3) VALUE 40.
PROCEDURE DIVISION.
CALL "INNER".
STOP RUN.
IDENTIFICATION DIVISION.
PROGRAM-ID. INNER.
DATA DIVISION.
PROCEDURE DIVISION.
DISPLAY "inner sees " WS-A.
GOBACK.
END PROGRAM INNER.
END PROGRAM MAINP.
cobc -x -free -Wall -o nestbad nestbad.cob
nestbad.cob:13: error: 'WS-A' is not defined
11 | DATA DIVISION.
12 | PROCEDURE DIVISION.
13 > DISPLAY "inner sees " WS-A.
14 | GOBACK.
15 | END PROGRAM INNER.
Data crosses the boundary the same way it crosses between two separate programs: through USING and a linkage section, or through an EXTERNAL item. The nesting buys the compiler a shorter call and gives the source file a way to keep a helper beside the program that uses it; the language keeps the data private.
Exercises
- Add a second nested program to the first file and call both from the parent. Which END PROGRAM closes what?
- Add an ENTRY "NEGATE" to the calculator that returns minus the amount, and call it.
- Declare WS-A of the failing program IS EXTERNAL in both programs and run it. What does the inner program print?
- Call "DOUBLE" in the calculator program and read the message of the run.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How is a program declared inside another one?
Answer
Before the END PROGRAM of the parent, closed by its own END PROGRAM
INNER sits between the divisions of MAINP and is closed before the parent.
-
What does ENTRY add?
Answer
Another name and place where a call can start the same program
The call to TRIPLE entered the calculator at the entry point and used the multiplication by three.
-
Can a nested program read the working storage of its parent?
Answer
No, it gets an undefined name, as the failing build shows
The error was WS-A is not defined, inside the nested program.
-
How does data reach a nested program?
Answer
Through USING and a LINKAGE SECTION, or an EXTERNAL item
The working program passed both fields in the USING list.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(3) VALUE 12. 01 W-R PIC 9(5) VALUE 0. PROCEDURE DIVISION. CALL "Q5SUB" USING W-A W-R. DISPLAY W-R. STOP RUN. IDENTIFICATION DIVISION. PROGRAM-ID. Q5SUB. DATA DIVISION. LINKAGE SECTION. 01 L-A PIC 9(3). 01 L-R PIC 9(5). PROCEDURE DIVISION USING L-A L-R. COMPUTE L-R = L-A * 10 GOBACK. END PROGRAM Q5SUB. END PROGRAM Q5.Answer
00120
The nested program multiplies the twelve by ten into the five-digit result.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9 VALUE 0. PROCEDURE DIVISION. CALL "Q6SUB". CALL "Q6SUB". STOP RUN. IDENTIFICATION DIVISION. PROGRAM-ID. Q6SUB. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-C PIC 9 VALUE 0. PROCEDURE DIVISION. ADD 1 TO W-C. DISPLAY W-C. GOBACK. END PROGRAM Q6SUB. END PROGRAM Q6.Answer
1 then 2
The working storage of the subprogram survives between calls, nested or not.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(3) VALUE 7. 01 W-B PIC 9(3) VALUE 3. PROCEDURE DIVISION. DISPLAY W-A " " W-B. STOP RUN. END PROGRAM Q7.Answer
007 003
Both fields are three digits wide, so the leading zeros are printed.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9 VALUE 1. PROCEDURE DIVISION. PERFORM UNTIL W-N IS GREATER THAN 2 DISPLAY "pass " W-N ADD 1 TO W-N END-PERFORM. STOP RUN. END PROGRAM Q8.Answer
pass 1 then pass 2
The loop runs for the values one and two.
-
Two nested programs are compiled into one module. What does that save?
Answer
A separate source file and a separate module at run time
The compiler keeps the nested program in the module of its parent.
-
Why does the call to "DOUBLE" fail in the calculator program?
Answer
The program is callable by its PROGRAM-ID and its ENTRY names only
The name DOUBLE is not one of the entry points of the program CALC.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev