Paragraphs and sections

A COBOL procedure division is a list of named blocks. The smallest is the paragraph, the larger is the section, and both are things a PERFORM can name. This lesson is the map of that structure; the next three are the ways it surprises a reader.

A paragraph and its name

A paragraph is a name on a line of its own, followed by statements and ended by the next name or the end of the program. The name is a label and a unit at once: it is what PERFORM calls, and it is where the flow lands when it falls through:

IDENTIFICATION DIVISION. PROGRAM-ID. PARA. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM HEADER-P. MOVE 7 TO N-F. PERFORM BODY-P. STOP RUN. HEADER-P. DISPLAY "report". BODY-P. DISPLAY "value " N-F. PERFORM FOOTER-P. FOOTER-P. DISPLAY "end".

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

report
value 7
end

BODY-P performs FOOTER-P before it returns to the main flow, which is how a paragraph calls another one. There is no declaration and no prototype: the name is resolved from the paragraphs of the program, in any order.

Sentences and periods

Inside a paragraph the statements are grouped into sentences, and a period ends one. The paragraph above has one sentence per paragraph because every period closes a sentence at the end of the block, and that is the habit to keep: a period at the end of a paragraph and END-IF, END-PERFORM and friends inside it.

The two levels exist because COBOL predates the block statement. A NEXT SENTENCE jumps to the next sentence and not to the next paragraph, which is the reason the statement is archaic and why the lesson on CONTINUE showed it skipping lines. A paragraph is the unit a modern program should use.

A section of several paragraphs

A section is a name with the word SECTION after it, and the paragraphs under it belong to it:

IDENTIFICATION DIVISION. PROGRAM-ID. SECT. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM REPORT-SECTION. DISPLAY "back in main". STOP RUN. REPORT-SECTION SECTION. HEAD-P. DISPLAY "header". BODY-P. ADD 1 TO N-F. DISPLAY "body " N-F. FOOT-P. DISPLAY "footer".

cobc -x -free -Wall -o sect section.cob
./sect

header
body 1
footer
back in main

PERFORM REPORT-SECTION ran all three paragraphs and returned after the last of them. A paragraph performed by name runs one block; a section performed by name runs the whole group, and that is the difference between the two forms.

Where a section ends

A section ends at the next section header, not at the last paragraph the writer had in mind. Two sections with a paragraph each show it, and the third paragraph is outside the first section and does not run:

IDENTIFICATION DIVISION. PROGRAM-ID. TWOSEC. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. PERFORM FIRST-SECTION. DISPLAY "after the first section, N is " N-F. STOP RUN. FIRST-SECTION SECTION. ONE-P. ADD 1 TO N-F. DISPLAY "one". TWO-P. ADD 10 TO N-F. DISPLAY "two". SECOND-SECTION SECTION. THREE-P. ADD 100 TO N-F. DISPLAY "three".

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

one
two
after the first section, N is 011

The counter collected one and ten and not a hundred, so THREE-P stayed out of the performed range. The same positional rule applies to PERFORM THRU, and the lesson on PERFORM THRU showed what it does to a range that grows a paragraph.

Paragraph and section names share one namespace in a program, so the same name cannot be used for a paragraph and for a section, and a name defined twice is a build error. The next lesson is the one that costs the most: what runs after a paragraph ends, and why a missing STOP RUN or a misplaced period changes a report.

Exercises

  1. Perform BODY-P twice from the main flow of the first program. How many lines print?
  2. Perform THREE-P by name from the main flow of the third program. Does the section matter?
  3. Add a second paragraph to SECOND-SECTION and perform the section. What prints now?
  4. Give a paragraph the same name as a section and build. What does the compiler say?

Questions

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

  1. What is a paragraph?

    Answer

    A name with statements under it, which PERFORM can call

    The name on its own line starts the block, and the next name or the end of the program ends it.

  2. What does PERFORM of a section run?

    Answer

    Every paragraph of the section, in source order

    The section performed on this page printed the header, the body and the footer.

  3. Where does a section end?

    Answer

    At the next SECTION header

    The performed section stopped before THREE-P, which was in the second section.

  4. What ends a sentence in COBOL?

    Answer

    A period

    The period is the sentence terminator, which is why it also closes every open IF.

  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 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.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. PERFORM A-SECTION. DISPLAY N-F. STOP RUN. A-SECTION SECTION. P-ONE. ADD 1 TO N-F. P-TWO. ADD 10 TO N-F. B-SECTION SECTION. P-THREE. ADD 100 TO N-F.

    Answer

    011

    The performed section runs its two paragraphs and stops before the second section.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. PERFORM P-TWO. DISPLAY N-F. STOP RUN. A-SECTION SECTION. P-ONE. ADD 1 TO N-F. P-TWO. ADD 10 TO N-F. B-SECTION SECTION. P-THREE. ADD 100 TO N-F.

    Answer

    010

    A paragraph performed by name runs only that paragraph, whatever section it belongs to.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. PERFORM ONE-P. DISPLAY "main " N-F. STOP RUN. ONE-P. MOVE 5 TO N-F. TWO-P. MOVE 9 TO N-F.

    Answer

    main 005

    The PERFORM returns when ONE-P ends, so TWO-P never runs and the field keeps five.

  9. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q9. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. MOVE 1 TO N-F. DISPLAY "before " N-F. ONE-P. ADD 1 TO N-F. DISPLAY "in " N-F.

    Answer

    before 001 / in 002

    Without STOP RUN the main flow falls into the paragraph, which prints once before the program ends.

  10. Why is a section worth using?

    Answer

    It groups paragraphs so one PERFORM runs the whole unit of work

    A report built from a header, a body and a footer is one section and one PERFORM.

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