Multiple functions in COBOL Linkage

176 Views Asked by At

I'm a C++/Python developer dabbling in COBOL, using open-cobol (cobc) on Linux.

I have several related functions which I would like to keep together. Normally, in COBOL, there is only one function per linkage section. What I currently do is

CALL "GEAR-FUNCS" USING DO-SOMETHING PARAMS...

and it executes the relevant function depending on the value of DO-SOMETHING. This reduces the number of files but it means that I have to pass the same number of parameters every time, even though some of them are not relevant.

The ultimate aim is to reduce the number of files on the compile line. I don't want to end up with one big main file and lots of little linkage files which I have to pull in every time. I would like to have just a few linkage files. Currently, the only methods I know of are either my function lookup or to create a library which contains all the linkage functions.

I was wondering whether there is a better way of doing this. For instance, is there something that will allow multiple linkage sections or procedure divisions in a single source?

1

There are 1 best solutions below

0
cup On

This is not an answer - the answer is in Can multiple Cobol subroutines be in the same modulehttps://stackoverflow.com/a/48949229/2041317. This is just a series of examples for reference. There are two functions defined - LN-FUNC and LN-OBJ-FUNC.

  1. The COBOL-85 multiple program method

    IDENTIFICATION DIVISION.
    PROGRAM-ID. LN-FUNC.       
    DATA DIVISION.       
    LINKAGE SECTION.
    77 VAR1 PIC 9(2).
    77 VAR2 PIC 9(2).
    77 RESULT PIC ZZ9.
    PROCEDURE DIVISION USING VAR1, VAR2, RESULT.
        COMPUTE RESULT = VAR1 + VAR2.
    *    This is the return statement
        GOBACK.
    END PROGRAM LN-FUNC.
    ********************************************
    IDENTIFICATION DIVISION.
    PROGRAM-ID. LN-OBJ-FUNC.
    DATA DIVISION.
    LINKAGE SECTION.
    01 VAR-OBJ.
       02 OBJ-VAR1 PIC 9(2).
       02 OBJ-VAR2 PIC 9(2).
    77 RESULT PIC ZZ9.
    PROCEDURE DIVISION USING VAR-OBJ, RESULT.
    *    Another function entry point
        COMPUTE RESULT = OBJ-VAR1 + OBJ-VAR2.
    *    Alternative to GOBACK.
        EXIT PROGRAM.
    END PROGRAM LN-OBJ-FUNC.
    
  2. Using nested programs. These need to be nested like

    (AAA (BBB (CCC)))

not

(AAA (BBB) (CCC))

Example

   IDENTIFICATION DIVISION.
   PROGRAM-ID. NESTED.
   PROCEDURE DIVISION.
       DISPLAY "SURPRISE - THIS GOT EXECUTED?".
  ********************************************
  * First nested linkage
  ********************************************
   IDENTIFICATION DIVISION.
   PROGRAM-ID. LN-FUNC.       
   DATA DIVISION.       
   LINKAGE SECTION.
   77 VAR1 PIC 9(2).
   77 VAR2 PIC 9(2).
   77 RESULT PIC ZZ9.
   PROCEDURE DIVISION USING VAR1, VAR2, RESULT.
       COMPUTE RESULT = VAR1 + VAR2.
  *    This is the return statement
       GOBACK.
  ********************************************
  * Second nested linkage
  ********************************************
   IDENTIFICATION DIVISION.
   PROGRAM-ID. LN-OBJ-FUNC.
   DATA DIVISION.
   LINKAGE SECTION.
   01 VAR-OBJ.
      02 OBJ-VAR1 PIC 9(2).
      02 OBJ-VAR2 PIC 9(2).
   77 RESULT PIC ZZ9.
   PROCEDURE DIVISION USING VAR-OBJ, RESULT.
  *    Another function entry point
       COMPUTE RESULT = OBJ-VAR1 + OBJ-VAR2.
  *    Alternative to GOBACK.
       EXIT PROGRAM.
   END PROGRAM LN-OBJ-FUNC.
   END PROGRAM LN-FUNC.
   END PROGRAM NESTED.
  1. Using entry

    IDENTIFICATION DIVISION.
    PROGRAM-ID. USE-ENTRY.
    DATA DIVISION.       
    LINKAGE SECTION.
    * For LN-FUNC
    77 VAR1 PIC 9(2).
    77 VAR2 PIC 9(2).
    77 RESULT PIC ZZ9.
    * For LN-OBJ-FUNC
    01 VAR-OBJ.
       02 OBJ-VAR1 PIC 9(2).
       02 OBJ-VAR2 PIC 9(2).
    PROCEDURE DIVISION.
    *    This never gets displayed
        DISPLAY "Starting multi".       
    *    This is a function entry point
        ENTRY "LN-FUNC" USING VAR1, VAR2, RESULT.
        COMPUTE RESULT = VAR1 + VAR2.
    *    This is the return statement
         GOBACK.
    *    Another function entry point
        ENTRY "LN-OBJ-FUNC" USING VAR-OBJ, RESULT.
        COMPUTE RESULT = OBJ-VAR1 + OBJ-VAR2.
    *    Alternative return statement
         EXIT PROGRAM.
    END PROGRAM USE-ENTRY.