CALL runs another program (a subprogram); CANCEL removes a dynamically called subprogram from memory and resets its state so the next CALL loads it again. Together they support dynamic loading and memory management when the program name is in a variable. For full CALL syntax and parameter passing see CALL; this page focuses on the CALL-and-CANCEL pair for dynamic subprograms.
| Term | Meaning |
|---|---|
| CALL | Transfers control to another program; can pass parameters with USING |
| CANCEL | Removes a dynamically called program from memory and resets its state |
| Dynamic call | CALL with program name in a variable; program loaded at runtime |
| Static call | CALL with literal program name; often linked at link-edit time |
With a dynamic CALL (program name in a data item), the subprogram is loaded when first called. It stays in memory for later calls. CANCEL unloads it: storage is freed, and the next CALL loads a fresh copy from disk. That fresh copy starts with initial values in working storage and no leftover state. Use CANCEL when you want to free memory, force a clean state on the next call, or ensure the subprogram is reloaded (e.g. after a copy was replaced).
12345678WORKING-STORAGE SECTION. 01 PGM-NAME PIC X(8) VALUE 'UTILPGM'. PROCEDURE DIVISION. CALL PGM-NAME USING WS-IN, WS-OUT ON EXCEPTION DISPLAY 'CALL failed' END-CALL.
123CANCEL PGM-NAME *> or CANCEL "UTILPGM" *> Subprogram is removed from memory; next CALL loads it again.
CANCEL has no effect if the program was never called or is already canceled. Only the calling program (or one above it in the call stack) should CANCEL a given program; you cannot cancel a program that is above you in the hierarchy.
1234567891011121314151617WORKING-STORAGE SECTION. 01 SUBPGM-NAME PIC X(20). 01 WS-INPUT PIC X(50). 01 WS-OUTPUT PIC X(100). PROCEDURE DIVISION. MAIN. MOVE 'VALIDATE-DATA' TO SUBPGM-NAME CALL SUBPGM-NAME USING WS-INPUT WS-OUTPUT ON EXCEPTION DISPLAY 'Validate program not found' NOT ON EXCEPTION DISPLAY 'Validation done' *> Free memory and reset; next CALL loads fresh CANCEL SUBPGM-NAME END-CALL GOBACK.
CALL is like asking a friend to do a task; they come over (load) and do it. If you don’t say “you can go home” (CANCEL), they stay. CANCEL means “go home and forget what we did.” Next time you call, they come again and start from scratch. That way you don’t have too many friends sitting in the room (memory), and each visit starts fresh.
1. What happens to a subprogram when you execute CANCEL?
2. CANCEL is most relevant when using which type of CALL?