A COBOL subprogram can return cleanly on the first call and fail on the second because a counter, switch, or table entry kept its old value in WORKING-STORAGE. Move the same item to LOCAL-STORAGE, and COBOL allocates a fresh copy for each call. That is the practical difference developers need before debugging a strange rerun or CALL problem.
Short rule: Working Storage persists for the run unit unless the program is initialised again. Local Storage is allocated for each invocation and freed when the program returns.
Working Storage vs Local Storage Comparison
| Question | Working Storage | Local Storage |
|---|---|---|
| Scope | Visible to the program that defines it. | Visible to the program or method invocation that defines it. |
| Initialisation | VALUE clauses are applied when the run unit starts, or when the program is reinitialised after CANCEL or INITIAL. |
VALUE clauses are applied on each call or method invocation. Without VALUE, the initial content is undefined. |
| Persistence between CALL statements | Usually keeps the last-used value between calls in the same run unit. | Does not keep values after return. A new copy is allocated for the next invocation. |
| When to use | Use for program-level state that must remain available across paragraphs or repeated calls. | Use for temporary work fields that should start fresh for each call, especially in reusable subprograms. |
What Working Storage Means in COBOL
WORKING-STORAGE SECTION is part of the COBOL Data Division. It defines fields that belong to the program and remain available while the program is active in the run unit. A batch driver that calls the same subprogram many times can therefore see old values if the subprogram keeps counters, switches, or save areas in Working Storage and does not reset them.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CALL-COUNT PIC 9(4) VALUE ZERO.
01 WS-LAST-CUSTOMER PIC X(10).
PROCEDURE DIVISION.
ADD 1 TO WS-CALL-COUNT
DISPLAY 'CALL COUNT=' WS-CALL-COUNT
GOBACK.
If this program is called three times in the same run unit, WS-CALL-COUNT can show 1, then 2, then 3. That is useful when the program intentionally tracks state. It is a bug when the field was meant to be temporary.
What Local Storage Means in COBOL
LOCAL-STORAGE SECTION defines fields that are allocated when the program or method is invoked and freed when it returns. This makes Local Storage a good fit for scratch variables in reusable routines, recursive-style logic, and programs that may run in multiple invocations.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 LS-ITEM-AMOUNT PIC 9(5) VALUE ZERO.
01 LS-WORK-FLAG PIC X VALUE 'N'.
PROCEDURE DIVISION.
MOVE 'Y' TO LS-WORK-FLAG
GOBACK.
On the next call, LS-WORK-FLAG is allocated again and the VALUE clause is applied again. That behavior is often safer for work fields that should not remember a previous transaction, customer, or input record.
CALL Behavior That Causes Runtime Errors
The easiest trap is a subprogram called repeatedly from a driver. A field in Working Storage keeps its last value, so a flag that should start as N might still be Y. A table index might still point past the last loaded entry. A previous customer number might leak into the next calculation.
Use Working Storage when that persistence is intentional. Use Local Storage when each call should start with a clean set of work fields. If a program uses PROGRAM-ID. name IS INITIAL, or if the caller issues CANCEL before calling again, Working Storage can be reinitialised, but that is a program-control decision and should not be hidden inside a variable naming habit.
Threading Difference
In environments where the same program can run in multiple simultaneous invocations, Working Storage can be shared by those invocations, while Local Storage gives each invocation a separate copy. That difference matters in CICS and other multi-tasking designs. If a field belongs to one transaction, request, or invocation, Local Storage is usually the safer place.
Where This Fits in the Data Division
Both sections belong to the COBOL Data Division, along with File Section and Linkage Section. For a broader layout of the Data Division, see the COBOL Data Division guide. If your bug is about fields passed between a caller and subprogram, also review the COBOL CALL statement and parameter passing guide.
Common Mistakes
- Putting a temporary transaction flag in Working Storage and forgetting to reset it before every call.
- Assuming Local Storage keeps a value after
GOBACK. - Using VALUE clauses as a substitute for explicit reset logic in a long-running program.
- Treating
CANCELas harmless when the called program relies on Working Storage persistence.
Practical Rule
If the value must survive the next call, use Working Storage and reset it deliberately when needed. If the value belongs only to this invocation, use Local Storage so old data cannot sneak into the next run path.
No comments:
Post a Comment