COBOL External Routines

COBOL can call external programs written in any language that follows standard linkage conventions. The external programs DO NOT have to be compiled within the same JOB as the COBOL calling program.

The LINKAGE EDITOR will link the calling program with the called program.

To call an external program, the CALL statement will be used.


Static Calling Format:  CALL  'pgm_name'  USING  PARM1  PARM2.
  
  - pgm_name is linked into the load module of the calling program
    during the linkage editor step


  pgm_name   this is the name of the program that is being called
             maximum of 8 characters
             COBOL: this is a PROGRAM-ID
             ASSEMBLER: this is a CSECT name
             

  USING      this signals that a parameter list will follow
      

  PARM1 PARM2..   these are the parameters that are being passed
                  to the external program



Dynamic Calling Format:   MOVE 'pgm-name' to ws-field.
                            CALL  ws-field  USING  PARM1  PARM2.
  
  - ws-field is a field in WORKING-STORAGE that has the external
    program name moved to it prior to calling the external program
  
  - pgm_name is linked into the load module of the calling program
    at run time
  
  - pgm_name must be a member of STEPLIB

 

Calling Program:

Does not know the language that the external program is written in.

Standard ASSEMBLER code for calling subprograms (routines) will be generated by the COBOL compiler.

  - a parameter list will be set up
  - the address of the parameter list will be put into register 1
  - the subprogram will be BALRed to

 

Called Program:

Does not know the language that the calling program is written in.

- the DATA DIVISION must contain a LINKAGE SECTION, which
  is where the parameter list will be described

    
       DATA DIVISION.
       
       WORKING-STORAGE SECTION.
       
          *** working storage code goes here ***
       
       LINKAGE SECTION.
       
         01  PARM-1           PIC X(30).
         01  PARM-2           PIC 9(10)  COMP-3.
         
         
  the LINKAGE SECTION is really just a DSECT describing the fields
  in the calling program's WORKING-STORAGE SECTION.  In
  other words, the data is not actually stored in the called
  program's LINKAGE SECTION.
    

  the parameter names in the LINKAGE SECTION do not have to be
  the same as the ones in the calling program.  The only 
  requirement is that the lengths of the data fields are the same.
    
    
       In calling program:
       
          01  NAME    PIC X(20)   VALUE 'STEVEN    TYLER'.
          
          
       In called program:
       
          01  NAME-FLD.
              05  FIRST-NAME   PIC X(10).
              05  LAST-NAME    PIC X(10).


    
- the PROCEDURE DIVISION statement must contain a USING statement 
  that lists the parameters being passed to the routine
    
  the parameters must be 01 level items in the LINKAGE SECTION

  the program must terminate with a GOBACK rather than a
  STOP RUN    

    
       PROCEDURE DIVISION USING PARM-1 PARM-2.
       
          *** procedure division code goes here ***
       
          GOBACK.