BY REFERENCE
Part of the Programs That Work Together section of Coddy's COBOL Programming journey. Lesson 5 of 16.
A field in a CALL ... USING list is passed by reference unless the call says otherwise. These two calls are the same:
CALL "UPCASE" USING CUST-NAME
CALL "UPCASE" USING BY REFERENCE CUST-NAMEBy reference, the subprogram's linkage field is the caller's field itself, not a copy. When the subprogram moves a value into it, the caller's field changes:
MOVE FUNCTION UPPER-CASE(LS-TEXT)
TO LS-TEXTThis is how a subprogram hands results back: the caller passes a field and reads it after the CALL.
A subprogram can also keep fields of its own in a WORKING-STORAGE SECTION. They belong to it alone and keep their values from one call to the next.
Challenge
EasyThe main program passes CUST-NAME by reference to UPCASE, but UPCASE puts the capitals in its own WS-TEXT, so the caller sees no change. Fix UPCASE.cob so ada lovelace prints [ADA LOVELACE ]
Example input, one line per ACCEPT: ada lovelace.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. NAMES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUST-NAME PIC X(20).
PROCEDURE DIVISION.
ACCEPT CUST-NAME
CALL "UPCASE" USING BY REFERENCE CUST-NAME
DISPLAY "[" CUST-NAME "]"
STOP RUN.