Skip to content
Subprograms with CALL
step 1/4

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 SECTION declares parameters — no memory; aliased to caller's data
  • PROCEDURE DIVISION USING ... lists what the subprogram receives
  • GOBACK returns to caller (use instead of STOP RUN in subprograms)
  • CALL "NAME" invokes by program-id (compile-time linked OR dynamic)

Parameter passing:

  • BY REFERENCE (default) — caller and callee share the same memory
  • BY 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-NAME lets 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…