Scope and fall-through

A paragraph is a label in the flow, not a function. Nothing in the language stops the main code from walking into the first paragraph when it reaches the last statement, and nothing stops a paragraph from walking into the next. This lesson is about the two flows and the one statement that separates them.

What PERFORM runs and what it returns to

PERFORM PART-A runs the statements of that paragraph, returns at its end and continues with the statement after the call:

IDENTIFICATION DIVISION. PROGRAM-ID. SCOPE. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM PART-A. DISPLAY "back in main". PERFORM PART-B. DISPLAY "done". STOP RUN. PART-A. ADD 1 TO N-F. DISPLAY "A, N is " N-F. PART-B. ADD 10 TO N-F. DISPLAY "B, N is " N-F.

cobc -x -free -Wall -o scope scope.cob
./scope

A, N is 1
back in main
B, N is 1
done

One call, one paragraph, one return. The second call shows that the range is the paragraph and nothing after it: PART-B did not run as part of PART-A and it did not run twice when it was called.

The main flow falls through

The main flow is not a paragraph and has no end of its own. When it reaches the last statement before the first paragraph header, it continues with that paragraph, and then with the next:

IDENTIFICATION DIVISION. PROGRAM-ID. FALL. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. DISPLAY "main starts". PERFORM PART-A. DISPLAY "main ends". PART-A. ADD 1 TO N-F. DISPLAY "A, N is " N-F. PART-B. ADD 10 TO N-F. DISPLAY "B, N is " N-F.

cobc -x -free -Wall -o fall fall.cob
./fall

main starts
A, N is 001
main ends
A, N is 002
B, N is 012

The report printed A twice and B once, and the counter ended at 12 instead of 1. The second A is the fall-through: after main ends the program walked into PART-A, and when that paragraph ended it walked into PART-B. The run ended when the program ran out of statements, which is legal and returns status zero.

EXIT does not return

The statement that looks like it should stop the walk is EXIT. It does nothing at all:

IDENTIFICATION DIVISION. PROGRAM-ID. EXITP. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. DISPLAY "main starts". PERFORM PART-A. DISPLAY "main ends". PART-A. ADD 1 TO N-F. DISPLAY "A, N is " N-F. EXIT. PART-B. ADD 10 TO N-F. DISPLAY "B, N is " N-F.

cobc -x -free -Wall -o exitp exitp.cob
./exitp

main starts
A, N is 001
main ends
A, N is 002
B, N is 012

Same output as the program without it. EXIT marks the end of a paragraph for PERFORM THRU, and the lesson on PERFORM THRU uses it for that; as a return statement it does not exist. EXIT PROGRAM returns from a subprogram and has nothing to do with this flow, and CALL and USING is where it belongs.

The rule that keeps it readable

The main flow ends with STOP RUN before the first paragraph, and every worker is reached only through a PERFORM. That one statement turns the paragraphs from things the flow can fall into into things it calls, and it is the reason every program on this course that has paragraphs ends its main flow with it.

A program that has no main flow at all, where the first paragraph is the entry point and the rest are performed, is the other safe shape and the one older code uses. A program that mixes the two without the statement is the one that prints a report paragraph twice, and the next lesson is the other way a range of paragraphs can surprise its writer.

Exercises

  1. Add STOP RUN after main ends in the fall-through program and count the lines. What is N-F at the end?
  2. Put a STOP RUN at the end of PART-A. What happens to the PERFORM PART-A in the main flow?
  3. Perform PART-A and then let the fall-through reach it. How many times does the counter increase, and why?
  4. Replace the EXIT in the third program with a GO TO to a paragraph after PART-B. What does the main flow do now?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Where does a PERFORM of one paragraph stop?

    Answer

    At the end of that paragraph, and control returns to the caller

    The performed range is one paragraph, so the statements after it are not part of the call.

  2. What happens when the main flow reaches the last statement before the first paragraph?

    Answer

    It falls into the paragraph and runs it

    The fall-through run printed A a second time and then B, without any PERFORM for either.

  3. What does EXIT do?

    Answer

    Nothing: it is a placeholder statement

    The program with EXIT printed exactly the same lines as the one without it.

  4. Which statement separates the main flow from the worker paragraphs?

    Answer

    STOP RUN before the first paragraph

    STOP RUN ends the program where the main flow ends, so the paragraphs are only reached by PERFORM.

  5. 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 ONE-P. DISPLAY "main " N-F. STOP RUN. ONE-P. MOVE 3 TO N-F. TWO-P. MOVE 7 TO N-F.

    Answer

    main 3

    The PERFORM returns at the end of ONE-P, so TWO-P never runs and STOP RUN keeps the flow out of it.

  6. 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 "main". MOVE 1 TO N-F. ONE-P. ADD 1 TO N-F. DISPLAY "one " N-F. TWO-P. ADD 2 TO N-F. DISPLAY "two " N-F.

    Answer

    main / one 2 / two 4

    Without STOP RUN the main flow falls into both paragraphs, adding one and then two to the field.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM ONE-P. DISPLAY "main " N-F. STOP RUN. ONE-P. ADD 1 TO N-F. EXIT. TWO-P. ADD 10 TO N-F.

    Answer

    main 1

    The EXIT is a no-op, and the PERFORM still returns at the end of ONE-P.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. DISPLAY "start". PERFORM P-ONE. PERFORM P-ONE. DISPLAY "end " N-F. STOP RUN. P-ONE. ADD 1 TO N-F.

    Answer

    start / end 2

    Two performed calls add two to the field, and the paragraph prints nothing itself.

  9. A report paragraph prints the header and the detail lines, and the header appears twice in the output. What is the first thing to check?

    Answer

    That the main flow ends with STOP RUN and cannot fall into the paragraph

    A duplicated first paragraph is the classic fall-through, which the second program on this page reproduces.

  10. Which shape keeps the paragraphs out of the main flow?

    Answer

    A main flow ending in STOP RUN, then the paragraphs, then nothing

    Only a statement that ends the run prevents the walk into the first paragraph.

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