Logic, files and calling other programs
COBOL's control flow is small: conditionals, PERFORM loops, EVALUATE for multi-way choices, and CALL to run another program. File I/O is verb-based and always tells you what happened through a status code.
EVALUATE: the readable multi-way branch
EVALUATE TRUE WHEN CUST-BALANCE > 100000 MOVE 'PREMIUM' TO WS-TIER WHEN CUST-BALANCE > 10000 MOVE 'STANDARD' TO WS-TIER WHEN CUST-CLOSED MOVE 'CLOSED' TO WS-TIER WHEN OTHER MOVE 'BASIC' TO WS-TIER END-EVALUATE
File verbs and FILE STATUS
Always declare FILE STATUS IS WS-STATUS on the SELECT and check it after every operation. Without it, an I/O failure gives you an abend with no context; with it, you know exactly which file and which condition.
| Status | Means |
|---|---|
| 00 | Successful |
| 02 | Successful, duplicate key on an alternate index |
| 10 | End of file |
| 22 | Duplicate key — the record already exists |
| 23 | Record not found |
| 35 | File not found at OPEN — usually a missing DD |
| 37 | Open failed on an incompatible device or format |
| 39 | Attribute mismatch between program and file |
| 9x | Vendor-specific; check the compiler's list |
Tables and subscripts
01 WS-RATE-TABLE.
05 WS-RATE-ENTRY OCCURS 50 TIMES
INDEXED BY WS-RATE-IX.
10 WS-RATE-CODE PIC X(03).
10 WS-RATE-VALUE PIC S9(3)V9(4) COMP-3.
SET WS-RATE-IX TO 1
SEARCH WS-RATE-ENTRY
AT END
DISPLAY 'RATE NOT FOUND: ' WS-IN-CODE
MOVE 16 TO RETURN-CODE
WHEN WS-RATE-CODE(WS-RATE-IX) = WS-IN-CODE
MOVE WS-RATE-VALUE(WS-RATE-IX) TO WS-RATE
END-SEARCHCalling another program
* in the caller 01 WS-PARM-AREA. 05 WS-IN-CODE PIC X(03). 05 WS-OUT-RATE PIC S9(3)V9(4) COMP-3. 05 WS-RETURN PIC S9(4) COMP. CALL 'RATELKUP' USING WS-PARM-AREA * in the called program RATELKUP LINKAGE SECTION. 01 LK-PARM-AREA. 05 LK-IN-CODE PIC X(03). 05 LK-OUT-RATE PIC S9(3)V9(4) COMP-3. 05 LK-RETURN PIC S9(4) COMP. PROCEDURE DIVISION USING LK-PARM-AREA.
- Static CALL — a literal name; the module is bound in at link time. Fast, but changing the called program means relinking the caller.
- Dynamic CALL — a variable name, or compiled with DYNAM; resolved at run time from the STEPLIB. Slower per call, but you can replace one module without relinking everything.
- Parameters are passed by reference by default: the called program changes the caller's storage directly.
Example, line by line
PERFORM UNTIL WS-EOF READ INFILE AT END SET WS-EOF TO TRUE NOT AT END PERFORM 2000-PROCESS END-READEND-PERFORMTry it yourself
Which statement should a called subprogram use to return control to its caller?
Show a hint
STOP RUN would end the whole run unit, not just the subprogram.
Show the solution
GOBACK. It returns to the caller; in a main program it also ends the run.
Common mistakes
Every I/O failure becomes a mysterious abend instead of a clear message naming the file and the condition.
There is no compile-time check across programs. A layout change on one side and not the other produces corrupted data, not an error.
Subscript errors that quietly corrupt storage in test become dramatic failures in production with more data.
Values persist between calls. Initialise at entry unless persistence is intended.
What you will see at work
- Most applications are a tree of small programs calling each other through copybook-defined parameter areas.
- Whether your site uses static or dynamic calls affects what you must relink after a change. Find out early — it is a common cause of 'my fix did not take effect'.
- A changed copybook means recompiling every program that includes it. Impact analysis tools exist for exactly this.
Key terms
Check your understanding.
Take this lesson's quiz and save your progress. Free.