Multiple Return Values in COBOL

COBOL, being a procedural language primarily used for business applications, doesn’t have a direct equivalent for multiple return values as seen in more modern languages. However, we can simulate this behavior using multiple parameters passed by reference. Here’s how we might approach this:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. MULTIPLE-RETURN-VALUES.
       
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-A PIC 9(5).
       01 WS-B PIC 9(5).
       01 WS-C PIC 9(5).
       
       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           PERFORM VALS-PROCEDURE.
           DISPLAY WS-A.
           DISPLAY WS-B.
           
           PERFORM VALS-PROCEDURE.
           DISPLAY WS-B.
           
           STOP RUN.
       
       VALS-PROCEDURE.
           MOVE 3 TO WS-A.
           MOVE 7 TO WS-B.

In this COBOL program:

  1. We define three working-storage variables: WS-A, WS-B, and WS-C.

  2. The VALS-PROCEDURE simulates returning multiple values by setting WS-A to 3 and WS-B to 7.

  3. In the MAIN-PROCEDURE, we call VALS-PROCEDURE and then display the values of WS-A and WS-B.

  4. To simulate using only a subset of the “returned” values, we call VALS-PROCEDURE again but only display WS-B.

COBOL doesn’t have a built-in concept of ignoring values (like the blank identifier _ in some languages). Instead, we simply don’t use the variable we’re not interested in.

To run this COBOL program:

$ cobc -x multiple-return-values.cob
$ ./multiple-return-values
3
7
7

This example demonstrates how to simulate multiple return values in COBOL. While it’s not as elegant as in more modern languages, it achieves a similar result by using variables in the working-storage section.

In COBOL, passing parameters by reference is a common practice for simulating multiple return values from a procedure. This approach is often used in business applications where COBOL is prevalent.