The PROCEDURE DIVISION
The procedure division is the part that does something. It is built from paragraphs, a paragraph is built from sentences, and a sentence is built from statements. Two of those three levels are invisible in a short program, and the third, the period, is where most first programs break.
A paragraph
A paragraph starts with a name of its own in area A and ends where the next paragraph name begins. The name is a label, not a call: writing it in the text does not run it, and a PERFORM is what transfers control.
IDENTIFICATION DIVISION.
PROGRAM-ID. PARA.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N PIC 9 VALUE 0.
PROCEDURE DIVISION.
PERFORM GREET.
DISPLAY "N = " N.
STOP RUN.
GREET.
DISPLAY "hello".
ADD 1 TO N.
cobc -x -free -Wall -o para para.cob
./para
hello
N = 1
The statements above GREET are the body of the program: they run first, and the paragraph below them runs only when something performs it. That shape, a body and the paragraphs it calls, is the one every program in this course uses.
Sentences and statements
A sentence ends with a period and may hold more than one statement. A statement needs no terminator of its own when the next word cannot continue it, which is why DISPLAY "hello". is a sentence of one statement and MOVE 1 TO N DISPLAY N. is a sentence of two. The period is the one separator the compiler cannot infer.
A paragraph name after an unterminated sentence is read as a word of that sentence, and the error that follows names the paragraph rather than the missing period:
IDENTIFICATION DIVISION.
PROGRAM-ID. NOEND.
PROCEDURE DIVISION.
DISPLAY "first"
DISPLAY "second"
SECOND.
DISPLAY "in second".
cobc -x -free -Wall -o noend noend.cob
noend.cob:8: error: 'SECOND' is not defined
6 | DISPLAY "second"
7 |
8 > SECOND.
9 | DISPLAY "in second".
10 | <EOF>
The message says the name is not defined because a DISPLAY may take a name as its operand, so the compiler read SECOND as a field it had never seen. The fix is the period at the end of the line above, not a declaration.
Falling off the end
A program does not need a STOP RUN: when the last statement of the program has run, control returns to the operating system with status 0. A program of one DISPLAY and no STOP RUN is a complete program.
IDENTIFICATION DIVISION.
PROGRAM-ID. FALL.
PROCEDURE DIVISION.
DISPLAY "the last statement runs".
cobc -x -free -Wall -o fall fall.cob
./fall
echo "status: $?"
the last statement runs
status: 0
What a missing STOP RUN does cost is the fall-through from the body into the paragraph below it. A program whose body ends without a period and without a STOP RUN keeps running into the next paragraph, which is the paragraph it was about to perform or had already performed:
IDENTIFICATION DIVISION.
PROGRAM-ID. THRU.
PROCEDURE DIVISION.
DISPLAY "main body".
GREET.
DISPLAY "greet paragraph".
cobc -x -free -Wall -o thru thru.cob
./thru
echo "status: $?"
main body
greet paragraph
status: 0
Both lines are printed by one run of a program that never asked for the paragraph. STOP RUN after the body is what ends the program at the place the writer meant, and the lesson on scope and fall-through reads this accident in full.
The USING of the header
A program that another program calls may take parameters, and the header says so: PROCEDURE DIVISION USING P. The name is declared in the linkage section, and the caller passes it in a CALL ... USING. Two programs may sit in one file, the first the main program and the second the one it calls.
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N PIC 9 VALUE 4.
PROCEDURE DIVISION.
CALL "DOUBLE" USING N.
DISPLAY "N = " N.
STOP RUN.
IDENTIFICATION DIVISION.
PROGRAM-ID. DOUBLE.
DATA DIVISION.
LINKAGE SECTION.
01 P PIC 9.
PROCEDURE DIVISION USING P.
COMPUTE P = P * 2.
EXIT PROGRAM.
END PROGRAM DOUBLE.
END PROGRAM MAINP.
cobc -x -free -Wall -o nested nested.cob
./nested
N = 8
EXIT PROGRAM returns to the caller; STOP RUN in the same place would end the whole run, and the DISPLAY after the call would never print. The difference between the two is the whole subject of CALL and USING.
A program that is never called writes no USING: the header is just PROCEDURE DIVISION., as in every program above.
Exercises
- Add a second paragraph to the first program that prints the value of N, and perform it after GREET. Which paragraph runs first, and why?
- Remove the period after the last statement of the GREET paragraph and build again. What does the compiler say, and which line does it name?
- Delete STOP RUN from the fall-through program and add a third paragraph below GREET. How many lines does the run print now?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What ends a sentence in the PROCEDURE DIVISION?
Answer
A period
A period ends a sentence and may hold several statements before it; a blank line and a paragraph name are not separators.
-
What is a paragraph?
Answer
A name with a period and the sentences under it, up to the next name
The name labels the paragraph; the sentences after it belong to it until the next paragraph name begins.
-
What does
EXIT PROGRAMdo in a called program?Answer
It returns control to the caller
EXIT PROGRAM returns to the statement after the CALL; STOP RUN would end the run and never return.
-
What does
PROCEDURE DIVISION USING Pdeclare?Answer
That the program takes one parameter, named P
USING lists the parameters, and each name is declared in the linkage section so the two descriptions agree with the caller.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 N PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM GREET. DISPLAY "N = " N. STOP RUN. GREET. DISPLAY "hello". ADD 1 TO N.Answer
hello / N = 1
The body runs first and performs GREET, which prints and adds one, so the label line comes after the greeting.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. PROCEDURE DIVISION. DISPLAY "main body". GREET. DISPLAY "greet paragraph".Answer
main body / greet paragraph
With no STOP RUN the body falls into the paragraph below it, and both DISPLAY statements run in one pass.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. PROCEDURE DIVISION. DISPLAY "the last statement runs".Answer
the last statement runs
Falling off the end of the program returns to the operating system with status 0, so STOP RUN is optional.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. PROCEDURE DIVISION. DISPLAY "first" DISPLAY "second" SECOND. DISPLAY "in second".Answer
error: 'SECOND' is not defined
Without the period the sentence runs on, DISPLAY reads SECOND as an operand, and the compiler reports a name it has never seen.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 N PIC 9 VALUE 4. PROCEDURE DIVISION. CALL "DOUBLE" USING N. DISPLAY "N = " N. STOP RUN. IDENTIFICATION DIVISION. PROGRAM-ID. DOUBLE. DATA DIVISION. LINKAGE SECTION. 01 P PIC 9. PROCEDURE DIVISION USING P. COMPUTE P = P * 2. EXIT PROGRAM. END PROGRAM DOUBLE. END PROGRAM Q5.Answer
N = 8
The called program receives the caller's byte through LINKAGE, doubles it in place, and EXIT PROGRAM returns to the DISPLAY.
-
A called program ends with
STOP RUNinstead ofEXIT PROGRAM. What happens?Answer
The whole run ends there and the caller never continues
STOP RUN terminates the run wherever it appears, so the statements after the CALL never execute.
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