A Small Example of Linkage

Suppose we have a main program called MAIN, written in COBOL. It is going to call a subprogram called MAKECODE, written in COBOL.

The idea is that MAKECODE will create a 7-character code value using the first 3 characters in Last-Name followed by the last 4 digits in Telephone. MAKECODE will have a return code of 0.


What do we have in MAIN?

In the Working-Storage Section of MAIN, we have:

       01  Last-Name               Pic X(15).
       01  Telephone               Pic 9(10).
       01  ID-Code                 Pic X(7).

In the Procedure Division of MAIN, we have:

           Call 'MAKECODE' using Last-Name  Telephone  ID-Code.

(Notice that this is a static call.)

(Notice we are passing all three arguments by reference.)


What do we have in MAKECODE?

In MAKECODE, we have a Linkage Section:

       Linkage Section.

       01  Last-Name.
           05  Name-First-3        Pic X(3).
           05  Name-Rest           Pic X(12).

       01  Telephone.
           05  Telephone-First-6   Pic X(6).
           05  Telephone-Last-4    Pic X(4).

       01  ID-Code.
           05  First-Part          Pic X(3)
           05  Last-Part           Pic X(4).

In the Procedure Division of MAKECODE, we have:

       Procedure Division using Last-Name  Telephone  ID-Code.

       000-Main.
           Move Name-First-3     to First-Part.
           Move Telephone-Last-4 to Last-Part.
           Move 0 to Return-Code.
           GoBack.

(Notice we use GoBack and not Stop Run. If we used Stop Run at the end of MAKECODE, it would end not only MAKECODE but also MAIN, which might be embarrassing. GoBack means "go back one level.")


What if we wanted to use a dynamic call?

To use a dynamic call, we would need to have a Pic(8) variable in MAIN with a name such as Sub-Name. Then we would use:

            Move 'MAKECODE' to Sub-Name.
            Call Sub-Name Using Last-Name  Telephone  ID-Code.


What if we wanted to pass some arguments by content?

In this example, Last-Name and Telephone are used strictly as input; we do not want to change their values. We can protect against accidentally changing their values by passing the arguments by content instead of by reference:

            Call 'MAKECODE' Using By Content Last-Name
                                  By Content Telephone
                                  By Reference ID-Code.


What if we wanted MAKECODE to be in assembly language instead?

There would be no changes at all to MAIN. The code for MAKECODE would look something like this, using some NIU macros:

         XSAVE  BR=12,SA=MAKESAVE,TR=NO
         LM 2,4,0(1)               Unload the parameter list.
         MVC  0(3,4),0(2)          Copy from the last name.
         MVC  3(4,4),7(3)          Copy from the telephone number.
         XRETURN RC=0,TR=NO

We could avoid using XSAVE and XRETURN by writing our own entry and exit linkage.


What could go wrong here?

We are assuming that the value in Last-Name is at least 3 non-blank characters long. If it is important that ID-Code should not contain any blanks, we could test for this and either (a) specify what to use if Last-Name is too short, or (b) provide a nonzero value as a return code to let MAIN know that something is wrong.