PERFORM: simple and inline
PERFORM is the call of the language that has no stack: it runs a paragraph or a group of statements and returns to the statement after it. It is also the loop, with the clauses that the next two lessons add. This lesson has the plain form and the inline one.
PERFORM a paragraph
A paragraph is a name in area A followed by statements and a period. PERFORM SHOW-HEADER runs the paragraph and continues with the next statement of the caller, whatever the order of the paragraphs in the source:
IDENTIFICATION DIVISION.
PROGRAM-ID. PERF.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TOTAL-F PIC 9(5) VALUE 0.
01 TIMES-N PIC 9 VALUE 0.
PROCEDURE DIVISION.
PERFORM SHOW-HEADER.
MOVE 120 TO TOTAL-F.
PERFORM SHOW-TOTAL.
MOVE 375 TO TOTAL-F.
PERFORM SHOW-TOTAL.
DISPLAY "paragraph ran " TIMES-N " times".
STOP RUN.
SHOW-HEADER.
DISPLAY "=== statement ===".
ADD 1 TO TIMES-N.
SHOW-TOTAL.
DISPLAY "total " TOTAL-F.
ADD 1 TO TIMES-N.
cobc -x -free -Wall -o perf perform.cob
./perf
=== statement ===
total 00120
total 00375
paragraph ran 3 times
The two paragraphs are written after STOP RUN and still run, because PERFORM finds them by name. Nothing in the language requires a paragraph to appear before its first use, and the course keeps the main flow at the top and the workers under it.
The same paragraph twice
The counter proves the second property: a paragraph is not a new program. Its working storage is the storage of the program, so TIMES-N kept its value between the calls and the two executions of SHOW-TOTAL added to the same field.
That is what makes a paragraph cheap and what makes it dangerous: a paragraph that leaves a field set for the next call is a state machine whether its writer meant it or not. The lesson on INITIALIZE and resetting a table is about the same effect on a larger scale.
The inline PERFORM
For a group of statements that is used once, a paragraph is noise. PERFORM without a name opens a group that END-PERFORM closes:
IDENTIFICATION DIVISION.
PROGRAM-ID. INLINEPERF.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A-F PIC 9(3) VALUE 0.
01 B-F PIC 9(3) VALUE 0.
PROCEDURE DIVISION.
PERFORM
MOVE 10 TO A-F
MOVE 20 TO B-F
DISPLAY "inside the inline PERFORM"
END-PERFORM.
DISPLAY "A " A-F " B " B-F.
STOP RUN.
cobc -x -free -Wall -o inlperf inlperf.cob
./inlperf
inside the inline PERFORM
A 010 B 020
Without a clause the group runs once. The clauses TIMES, UNTIL and VARYING turn the same shape into a loop, and the next lesson is about them. The group needs END-PERFORM and a period does not close it: a period inside the group ends the sentence and the statement with it, and the compiler reports the PERFORM as not terminated.
Falling into a paragraph
Paragraphs are not protected from the flow of the program. Without STOP RUN the last statement of the main code runs into the first paragraph, which then runs a second time:
IDENTIFICATION DIVISION.
PROGRAM-ID. FALLTHRU.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N-F PIC 9 VALUE 0.
PROCEDURE DIVISION.
DISPLAY "main starts".
PERFORM STEP-ONE.
DISPLAY "main ends".
STEP-ONE.
ADD 1 TO N-F.
DISPLAY "step one, N " N-F.
cobc -x -free -Wall -o fallthru fallthru.cob
./fallthru
main starts
step one, N 1
main ends
step one, N 2
The second step one is the fall-through: the paragraph ran once from the PERFORM and once because the program walked into it. The run ended by reaching the end of the source, which is legal COBOL and returns status zero, so nothing warned. STOP RUN before the first paragraph, or an EXIT PROGRAM in a subprogram, is what separates the main flow from the workers.
Exercises
- Perform SHOW-HEADER once more at the end of the main flow. What does the counter print?
- Move SHOW-TOTAL above SHOW-HEADER in the source. Does the output change?
- Put a STOP RUN at the end of the main flow of the fall-through program and run it again. How many lines print?
- Write the inline group as a paragraph and perform it. Which version has fewer lines, and which one would you keep in a program with ten small steps?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where does control go after a performed paragraph finishes?
Answer
To the statement after the PERFORM that called it
PERFORM returns to its caller, which is why the paragraph can be placed anywhere in the source.
-
What does a paragraph share with the code that performs it?
Answer
The working storage of the program
The counter kept its value between calls, so the paragraph added to the same field each time.
-
How does an inline PERFORM end?
Answer
With END-PERFORM, and a period is not required
The inline form is closed by its scope terminator, which also carries the loop clauses in the next lesson.
-
What happens when the main code has no STOP RUN and a paragraph follows?
Answer
Execution falls into the paragraph and runs it again
The fall-through run printed step one a second time, and reaching the end of the source ended the program with status zero.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM ADD-ONE. PERFORM ADD-ONE. DISPLAY N-F. STOP RUN. ADD-ONE. ADD 1 TO N-F.Answer
2
Both calls add to the same field, and the value is printed once after them.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. DISPLAY "start". PERFORM STEP. DISPLAY "end". STEP. DISPLAY "step".Answer
start / step / end / step
After end the flow falls into STEP, so it prints once more before the program reaches the end of the source.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-F PIC 9(2) VALUE 0. PROCEDURE DIVISION. PERFORM MOVE 7 TO A-F DISPLAY "set" END-PERFORM. DISPLAY A-F. STOP RUN.Answer
set / 07
The inline group runs once, prints set, and then the DISPLAY writes the field it set.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 5. PROCEDURE DIVISION. PERFORM SHOW. STOP RUN. SHOW. DISPLAY "N is " N-F. MOVE 0 TO N-F.Answer
N is 5
The paragraph prints before it clears the field, and STOP RUN ends the program before any fall-through.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q9. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 1. PROCEDURE DIVISION. DISPLAY "before". PERFORM TWICE-ONE. STOP RUN. TWICE-ONE. ADD 1 TO N-F. DISPLAY N-F.Answer
before / 2
The paragraph adds to 1 and prints 2 once, and STOP RUN keeps the flow out of the paragraph afterwards.
-
A program prints one of its report lines twice. What is the first thing to check?
Answer
Whether STOP RUN ends the main flow before the paragraphs
A missing STOP RUN lets the main flow fall into the worker paragraphs and print their lines again.
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