TExcelTAD
Home › COBOL › COBOL PERFORM Paragraphs & Sections

COBOL PERFORM Paragraphs & Sections

PERFORM is how COBOL structures a program. It runs a paragraph (or section) and then returns to the statement after the PERFORM. A well-written COBOL program is a short main paragraph that performs one paragraph per task.

Performing a paragraph

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. PERFORM-BASIC.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-TOTAL           PIC 9(5)V99 VALUE 0.
       PROCEDURE DIVISION.
       0000-MAIN.
           PERFORM 1000-INITIALISE
           PERFORM 2000-PROCESS
           PERFORM 3000-FINISH
           STOP RUN.

       1000-INITIALISE.
           DISPLAY "Initialising"
           MOVE ZERO TO WS-TOTAL.

       2000-PROCESS.
           DISPLAY "Processing"
           ADD 19.99 TO WS-TOTAL
           ADD 5.01 TO WS-TOTAL.

       3000-FINISH.
           DISPLAY "Total: " WS-TOTAL.
Initialising
Processing
Total: 00025.00

The numeric prefixes (1000-, 2000-) are a widespread convention: they show the call hierarchy and make it easy to find a paragraph in a long listing. 1000-INITIALISE might itself perform 1100-OPEN-FILES and 1200-READ-FIRST.

Note: COBOL paragraphs have no parameters and no local variables. They all share the data in the DATA DIVISION. Real parameter passing happens between programs with CALL … USING (later lesson).

PERFORM THRU

PERFORM A THRU C runs paragraphs A, B and C in source order and returns after C. It is common in older code, usually with an empty "exit" paragraph that marks the end of the range:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. PERFORM-THRU.
       PROCEDURE DIVISION.
       MAIN-PARA.
           PERFORM VALIDATE-START THRU VALIDATE-EXIT
           DISPLAY "Back in MAIN-PARA"
           STOP RUN.

       VALIDATE-START.
           DISPLAY "Checking account".
       VALIDATE-AMOUNT.
           DISPLAY "Checking amount".
       VALIDATE-EXIT.
           EXIT.
Checking account
Checking amount
Back in MAIN-PARA

EXIT does nothing; it just gives the range a well-defined last paragraph. Many modern style guides avoid THRU because inserting a paragraph in the middle silently changes what runs.

Inline PERFORM

COBOL-85 added the inline PERFORM … END-PERFORM, which runs the statements written inside it. On its own it just groups statements; its real power comes with the loop options in the next lesson:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. INLINE-PERFORM.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-N               PIC 9       VALUE 0.
       PROCEDURE DIVISION.
           PERFORM 3 TIMES
               ADD 1 TO WS-N
               DISPLAY "Inline step " WS-N
           END-PERFORM
           STOP RUN.
Inline step 1
Inline step 2
Inline step 3

Sections

Performing a section runs all of its paragraphs. Some shops structure everything in sections, and use EXIT SECTION to leave a section early:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SECTIONS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-AMOUNT          PIC S9(5)   VALUE -40.
       PROCEDURE DIVISION.
       A000-MAIN SECTION.
           PERFORM B000-CHECK-AMOUNT
           MOVE 75 TO WS-AMOUNT
           PERFORM B000-CHECK-AMOUNT
           STOP RUN.

       B000-CHECK-AMOUNT SECTION.
       B010-SIGN.
           IF WS-AMOUNT < 0
               DISPLAY "Rejected: negative amount"
               EXIT SECTION
           END-IF.
       B020-ACCEPT.
           DISPLAY "Accepted: " WS-AMOUNT.
Rejected: negative amount
Accepted: +00075

EXIT PARAGRAPH works the same way for paragraphs.

GO TO (and why to avoid it)

GO TO paragraph jumps without returning. Old programs used it everywhere, which created "spaghetti code". Modern COBOL uses PERFORM, EVALUATE and inline loops instead; the one common remaining use is GO TO the exit paragraph of a PERFORM … THRU range.

cobol
      *> Legacy pattern you will meet in existing code
       2000-PROCESS.
           IF WS-RECORD-TYPE NOT = "D"
               GO TO 2000-EXIT
           END-IF
           PERFORM 2100-APPLY-DETAIL.
       2000-EXIT.
           EXIT.

A common mistake: falling into paragraphs

If the main paragraph forgets STOP RUN (or GOBACK), execution continues into the next paragraph in source order. Here SHOW-TOTAL runs twice, once performed and once by falling through:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. FORGOT-STOP.
       PROCEDURE DIVISION.
       MAIN-PARA.
           PERFORM SHOW-TOTAL
           DISPLAY "Main finished".
       SHOW-TOTAL.
           DISPLAY "Showing total".
Showing total
Main finished
Showing total

Test yourself

What happens after a performed paragraph finishes?

  1. The program stops
  2. Control returns to the statement after the PERFORM
  3. The next paragraph runs
  4. It is performed again

What does PERFORM P1 THRU P3 run?

  1. P1 and P3 only
  2. P1, every paragraph between in source order, and P3
  3. P3 then P1
  4. P1 three times

Which statement leaves the current section early?

  1. STOP SECTION
  2. EXIT SECTION
  3. BREAK
  4. GOBACK SECTION