Open In App

Subroutines in COBOL

Subroutines in COBOL are small programs that are compiled independently but cannot be run directly. Cobol subroutine is a small program that can be compiled independently but cannot be run directly. There are two types of COBOL  subroutines- internal subroutines and the external subroutines

Call Verb

Call verb is used especially for transferring the controlling from one program to running another program on it. The program that is sourcing the CALL verb is the directly Calling Program and the program being specially called is called as the Called Program.



Calling program execution will halt until the controlling called program, thus it finishes the execution. Exit Program statement so, working in the Called program for transferring the controlling to back

Called Program Requirements

Types of Parameters Passing

Call By Reference: If the values of variables mentioned in the called program are modified, then their new values will reflect in the controlling calling program. If the BY clause is not mentioned, then more variables are always passing by reference from within it.



Syntax:

CALL sub-prog-name USING variable-1, variable-2

Call By Content: In call by content, if more values of variables directly in the subprogram(calling program) are controlling mentioned then their new values added will not be reflected in the main calling program(called program).

Syntax:

CALL sub-prog-name USING 
BY CONTENT variables-1, BY CONTENT variables-2

Types of Call

Static call: Static call arises controlling mentioned when the compiler compiles a program with NODYNAM option In the static call, all the modules are available in the controlling main memory. Hence, the processing becomes fast and it does not require additional timing to load it. Static call statement working mentioned the subprograms naming as literal, i.e in quotes

Dynamic call: Dynamic calls, are arising when the compiler compiles invoking a program with this DYNAM option. In the dynamic call, calling and added called programs within available loads are available separately. The size of the module is small because it requires controlling less memory while loading into the working main memory call processing is a little slower than static loading and unloading of main programs and subprograms in memory to decrease the speed of execution

Example 1:

Main file:




IDENTIFICATION DIVISION.
  PROGRAM-ID. MAIN.
  
  DATA DIVISION.
   WORKING-STORAGE SECTION.
      01 WS-STUDENT-ID PIC 9(4) VALUE 2000.
      01 WS-STUDENT-NAME PIC A(15) VALUE 'Gfg'.
  
  PROCEDURE DIVISION.
  CALL 'UTIL' USING WS-STUDENT-ID, WS-STUDENT-NAME.
  DISPLAY 'Student Id : ' WS-STUDENT-ID
  DISPLAY 'Student Name : ' WS-STUDENT-NAME
  STOP RUN.

Subroutine:




IDENTIFICATION DIVISION.
  PROGRAM-ID. UTIL.
  
  DATA DIVISION.
   LINKAGE SECTION.
      01 LS-STUDENT-ID PIC 9(4).
      01 LS-STUDENT-NAME PIC A(15).
  
  PROCEDURE DIVISION USING LS-STUDENT-ID, LS-STUDENT-NAME.
      DISPLAY 'In Called Program'.
      MOVE 1111 TO LS-STUDENT-ID.
  EXIT PROGRAM.

Output:

Student Id : 2000
Student Name : Gfg

Article Tags :