Reading — step 1 of 4
Learn
~1 min readMemory and Subprograms
COBOL programs can be split into multiple files. The CALL statement invokes another program.
*> Main program (main.cbl)
IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INPUT-VAL PIC 9(5).
01 RESULT PIC 9(7).
PROCEDURE DIVISION.
MOVE 5 TO INPUT-VAL.
CALL "SQUARE" USING INPUT-VAL, RESULT.
DISPLAY "squared: ", RESULT.
STOP RUN.
*> Subprogram (square.cbl)
IDENTIFICATION DIVISION.
PROGRAM-ID. SQUARE.
DATA DIVISION.
LINKAGE SECTION. *> data passed FROM caller — no allocation
01 N PIC 9(5).
01 R PIC 9(7).
PROCEDURE DIVISION USING N, R.
COMPUTE R = N * N.
GOBACK. *> return to caller
Key details:
LINKAGE SECTIONdeclares parameters — no memory; aliased to caller's dataPROCEDURE DIVISION USING ...lists what the subprogram receivesGOBACKreturns to caller (use instead ofSTOP RUNin subprograms)CALL "NAME"invokes by program-id (compile-time linked OR dynamic)
Parameter passing:
BY REFERENCE(default) — caller and callee share the same memoryBY CONTENT— pass a copy (changes don't propagate back)BY VALUE— modern; pass-by-value (numeric types only)
CALL "SUBPROG" USING BY VALUE INPUT-VAL
BY REFERENCE OUTPUT-VAL.
Static vs dynamic calls:
- Static — compile-time linked; faster, larger executable
- Dynamic —
CALL VARIABLE-NAMElets you decide at runtime; loaded on first call
For Judge0 (single-file submissions), embed everything in one program. Real mainframe systems have hundreds of small programs called by orchestrators.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…