Subroutines


A subroutine is a block of code that is not compiled or executed independently. These subroutines can exist within or outside of the application program.

Subroutines are one of the following -

  • A paragraph separated from procedure division (within the program).
  • A piece of code that is separated from the program and used in multiple programs (COBOL includes).
  • A program is called from the main program (subprogram).

There are two types of subroutines -

  • Internal subroutines.
  • External subroutines
    • Subprograms.
    • PROCEDURE DIVISION copybooks.

Internal subroutines -


These are paragraphs or sections that are coded within the program and can be called by using the PERFORM statement. These can be coded anywhere in the program after the STOP RUN statement. For Example -

       ...
       PROCEDURE DIVISION.
           PERFORM 1000-PARA.
           STOP RUN.
		   
	   1000-PARA.
	       ACCEPT WS-AGE.
           IF WS-AGE >= 18
               DISPLAY "Person is an adult."
           ELSE
               DISPLAY "Person is not an adult."
           END-IF.

External subroutines -


These are the code blocks that can code outside of the program and can be used in multiple programs to complete a task. These code blocks can be a pragraph or section or subprogram.

These are two types -

  • Subprograms
  • PROCEDURE DIVISION Copybooks

Subprograms -


These are separate modules coded to perform a specific task and can be called by multiple programs. These modules can't execute individually and should be called from another module.

The subprogram concept is well explained in the previous chapter Program Communication Statements.

PROCEDURE DIVISION Copybooks


These are paragraphs or sections that are added during the program compilation. INCLUDE statement used to insert a code block into a source program during the program compilation. The code block contains the PROCEDURE DIVISION code in it. These are called COBOL includes or PROCEDURE DIVISION copybooks.

Example - Using ACCINPUT (PROCEDURE DIVISION copybook) in the COBOL Program.

Program Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. COBINC.
	   AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-NAME              PIC X(20).
          05 WS-GENDER            PIC X(01).

       PROCEDURE DIVISION.

           PERFORM 1000-RECEIVE-INPUTS.
           DISPLAY 'RECEIVED NAME:    ' WS-NAME.
           DISPLAY 'RECEIVED GENDER:  ' WS-GENDER.

           STOP RUN.
		   
           ++INCLUDE ACCINPUT.

ACCINPUT code copybook -

       1000-RECEIVE-INPUTS.
           ACCEPT WS-NAME.
           ACCEPT WS-GENDER.

Run JCL -

//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID
//*
//STEP01  EXEC PGM=COBINC
//STEPLIB  DD  DSN=MATEPK.COBOL.LOADLIB,DISP=SHR 
//SYSIN    DD  *
SRINIVAS
M
/*
//SYSOUT   DD  SYSOUT=*

Output -

RECEIVED NAME:    SRINIVAS
RECEIVED GENDER:  M
Learn More?