---------------------------------------------------------------------------------------------
'Compile, link and go' is a program development process normally used when creating programs.

 Most programs are compiled and linked and stored in a load library.

 These programs are executed at some later time.
---------------------------------------------------------------------------------------------
A LOAD LIBRARY is a PDS containing machine language programs.

 The DCB or a load module library is
 RECFM=U, LRECL=0 and BLKSIZE=6144 or greater.

Load libraries are created like any other library --  use JCL or TSO/ISPF 3.2

---------------------------------------------------------------------------------------------
There are two options for putting load modules into a load library:

      (use PROC member COB2CLJ of SYS1.PROCLIB
       or use CL member from JPETLIC.CSC.CNTL)

---------------------------------------------------------------------------------------------
A program within a load library can be executed by JCL.

Use the JCL 'EXEC' to start a program

i.e. //stepname EXEC  PGM=pgmname

MVS will search the member names in SYS1.LINKLIB as the default load library when looking for pgmname.  In order to execute, pgmname should be a name of a member in SYS1.LINKLIB.
---------------------------------------------------------------------------------------------
To execute a program saved in a library other than SYS1.LINKLIB, use the STEPLIB DD statement after an EXEC statement to help the system find the library that contains pgmname.
 

i.e. //stepname EXEC  PGM=pgmname
     //STEPLIB DD DSN=load-library-name,DISP=SHR
 

--------------------------------------------------------------------------------------------
A program can also be started by a 'CALL' statement.

          CALL  'pgmname2'
 

-----------------------------------------------------------------------------------------
Whether a program is 'CALL'ed or 'EXEC'ed, it must be found by the system.  If the calling and called program are in the same load library, then a single STEPLIB can guide the system to look in one place for both the calling and called program.

i.e. //stepname EXEC  PGM=calling-program
     //STEPLIB DD DSN=load-library-name,DISP=SHR

------------------------------------------------------------------------------------------
If the calling and called program are in different load libraries, then each different load library that contains the calling and called programs should be listed with a concatenated JCL statement.

i.e. //stepname EXEC  PGM=calling-program
     //STEPLIB DD DSN=load-library-name1,DISP=SHR
     //        DD DSN=load-library-name2,DISP=SHR
     //        DD DSN=load-library-name3,DISP=SHR

------------------------------------------------------------------------------------------
Can a program, compiled from the following source code, be CALLED?
What would happen if it is CALLED from another program?
Is any information passed between the CALLING and CALLED programs?

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ADDER.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  VALUE-1      PIC 9999  VALUE IS 1234.
       01  VALUE-2      PIC 99    VALUE IS 99.
       01  VALUE-3      PIC 99999.
       PROCEDURE DIVISION.
       COMPUTE VALUE-3 = VALUE-1 + VALUE-2.
           DISPLAY VALUE-1 ' + ' VALUE-2 ' = ' VALUE-3.
           GOBACK.

---------------------------------------------------------------------------------------------
In order for the CALLED program to process information to and from another program,
 

In order for the CALLING program to process information to and from another program,

---------------------------------------------------------------------------------------------
The following shows how ADDER is to be modified in order to accept and return information from another program,

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ADDER2.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       LINKAGE SECTION.
       01  VALUE-1      PIC 9999.
       01  VALUE-2      PIC 99.
       01  VALUE-3      PIC 99999.
       PROCEDURE DIVISION USING VALUE-1 VALUE-2 VALUE-3.
       COMPUTE VALUE-3 = VALUE-1 + VALUE-2.
           DISPLAY VALUE-1 ' + ' VALUE-2 ' = ' VALUE-3.
           GOBACK.

There are no VALUE statements in the Linkage section.
Each item is expected as input-from or output-to another program.
--------------------------------------------------------------------------------------------
The following shows the calling program.

 IDENTIFICATION DIVISION.
       PROGRAM-ID. MAIN2.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  A      PIC 9999.
       01  B      PIC 99.
       01  C      PIC 99999.
       PROCEDURE DIVISION.
           MOVE 2123 TO A.
           MOVE 56 TO B.
           CALL 'ADDER2' USING A B C
           DISPLAY A ' + ' B ' = ' C
           GOBACK.
 
 Note:  The calling program must

Note:  The calling program does not have to use the same field names as the called program.

--------------------------------------------------------------------------------------------
The following shows that ADDER may modify any field in the LINKAGE SECTION.
 
       IDENTIFICATION DIVISION.
       PROGRAM-ID. ADDER3.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       LINKAGE SECTION.
       01  VALUE-1      PIC 9999.
       01  VALUE-2      PIC 99.
       01  VALUE-3      PIC 99999.
       PROCEDURE DIVISION USING VALUE-1 VALUE-2 VALUE-3.
           COMPUTE VALUE-3 = VALUE-1 + VALUE-2.
           DISPLAY VALUE-1 ' + ' VALUE-2 ' = ' VALUE-3.
           MOVE ZEROS TO VALUE-1 VALUE-2.
           GOBACK.
---------------------------------------------------------------------------------------
To ensure that only field C gets modified the by called program, the caller should label fields used in the calls BY CONTENT or BY REFERENCE.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. MAIN3.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  A      PIC 9999.
       01  B      PIC 99.
       01  C      PIC 99999.
       PROCEDURE DIVISION.
           MOVE 22 TO A.
           MOVE 33 TO B.
           CALL 'ADDER3' USING A B C
           DISPLAY A ' + ' B ' = ' C
           MOVE 2123 TO A.
           MOVE 56 TO B.
           CALL 'ADDER3' USING
                         BY CONTENT A B
                         BY REFERENCE C
           DISPLAY A ' + ' B ' = ' C
           GOBACK.

BY CONTENT means that only a copy of the contents of A and B are sent to the called program.  Any changes to the content of A or B in the called program will not come back into the calling program.

BY REFERENCE means that the data item is passed by address.  Any values put here by the called program will come back to the calling program.
---------------------------------------------------------------------------------------------
ADDER, and any other subprogram, is put into its INITIAL state when first called but not for subsequent calls.

INITIAL state means that the VALUE clauses are set and the PERFORM statements start with their initial values.

When the program is called subsequently, it will run with whatever values it had the last time it was called.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ADDER4.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  VALUE-4      PIC 99999 VALUE ZEROS.
       LINKAGE SECTION.
       01  VALUE-1      PIC 9999.
       01  VALUE-2      PIC 99.
       01  VALUE-3      PIC 99999.
       PROCEDURE DIVISION USING VALUE-1 VALUE-2 VALUE-3.
           ADD VALUE-1 VALUE-2 TO VALUE-4.
           MOVE VALUE-4 TO VALUE-3.
           GOBACK.
-----------------------------------------------------------------------------------------------------------------------------
      IDENTIFICATION DIVISION.
      PROGRAM-ID. MAIN4.
      . . .  Same content as main programs used earlier.
      WORKING-STORAGE SECTION.
      01  A      PIC 9999.
      01  B      PIC 99.
      01  C      PIC 99999.
      PROCEDURE DIVISION.
           MOVE 22 TO A.
           MOVE 33 TO B.
           CALL 'ADDER4' USING
                         BY CONTENT A B
                         BY REFERENCE C
           DISPLAY A ' + ' B ' = ' C
           MOVE 11 TO A.
           MOVE 22 TO B.
           CALL 'ADDER4' USING
                         BY CONTENT A B
                         BY REFERENCE C
           DISPLAY A ' + ' B ' = ' C
           CANCEL 'ADDER4'   ? this resets ADDER4 to its INITIAL state
           MOVE 11 TO A.
           MOVE 22 TO B.
           CALL 'ADDER4' USING
                         BY CONTENT A B
                         BY REFERENCE C
           GOBACK.

The subprogram could also puts itself into initial state for every CALL by:

PROGRAM-ID. ADDER4 INITIAL.
-------------------------------------------------------------------------------------------
The calls can be made dynamic by using a field-name to contain the name of the program called.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. MAIN5.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  A      PIC 9999.
       01  B      PIC 99.
       01  C      PIC 99999.
       01  PGM-NAME PIC X(8).
       PROCEDURE DIVISION.
           MOVE 2123 TO A
           MOVE 56 TO B
           MOVE 'ADDER4' TO PGM-NAME
           CALL PGM-NAME USING BY CONTENT A B <-- dynamic call
                               BY REFERENCE C
           DISPLAY A ' + ' B ' = ' C
           GOBACK.
-------------------------------------------------------------------------------------------------------