Menu
CoddyTech

LINKAGE SECTION

Part of the Programs That Work Together section of Coddy's COBOL Programming journey. Lesson 4 of 16.

USING hands fields to a subprogram. The caller lists its own fields:

CALL "ADDTAX" USING PRICE TOTAL

The subprogram describes what it receives in its LINKAGE SECTION. A linkage field has no storage of its own: it names the caller's field. It takes no VALUE.

LINKAGE SECTION.
01 LS-PRICE  PIC 9(4)V99.
01 LS-TOTAL  PIC 9(5)V99.

The subprogram's PROCEDURE DIVISION USING lists its linkage fields:

PROCEDURE DIVISION USING LS-PRICE LS-TOTAL.

Fields are matched by position, not by name: the first field of the CALL is the first of the USING list. Their pictures must match too, because the subprogram reads the caller's bytes with its own picture. GnuCOBOL stops a call whose linkage field is longer than the caller's; one of the same length with another picture gives wrong numbers, with no error.

challenge icon

Challenge

Easy

The main program calls ADDTAX with PRICE, PIC 9(4)V99, and TOTAL, PIC 9(5)V99. In ADDTAX.cob, declare LS-PRICE and LS-TOTAL in a LINKAGE SECTION and add the USING list, so 100 prints With tax:    108.00

Example input, one line per ACCEPT: 100.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. TAXMAIN.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 PRICE        PIC 9(4)V99.
       01 TOTAL        PIC 9(5)V99 VALUE ZERO.
       01 SHOW-TOTAL   PIC ZZ,ZZ9.99.
       PROCEDURE DIVISION.
           ACCEPT PRICE
           CALL "ADDTAX" USING PRICE TOTAL
           MOVE TOTAL TO SHOW-TOTAL
           DISPLAY "With tax: " SHOW-TOTAL
           STOP RUN.

All lessons in Programs That Work Together