Subroutines in COBOL

Introduction

  • Subroutines in COBOL is a program compiled independently, but cannot be executed independently.

  • There are two types of subroutines:
    1. Internal subroutines (Perform statement)
    2. External subroutines (CALL verb)

  • The subprogram is beneficial when the user performs a particular task(same task) in more than one program.

Call verb

  • Call verb is used to call the subprogram for performing the task.
  • To complete a particular task, Call verb transfers the control from one object to another.
  • The program which contains the CALL verb is known as Calling program or main program and the program which name is associated with CALL statement is known as Called program or subprogram.
  • Main program execution will halt till the subprogram finishes the execution.
  • The exit program statement is used in the Called program to transfer the control back.

Called Program Requirements

  • In called program, it is necessary to define Linkage section. It includes data element passed in the program. The data item does not have value clause. PIC clause is compatible with the variables passed through the calling program.
  • Procedure division contains a list of variables passed from the calling program and the order of the variable is same as mentioned in the call verb.
  • The exit program statement is used in called program to transfer the control back and it is the last statement in the called program.

Types of parameters passing

1) Call By Reference
  • In call by reference, the value of variables in the called program is modified, then it is reflected in the calling program.
  • The modifications in a subprogram or called program are global i.e visible to the main program.
  • Variables are always passed by reference when BY clause is not specified.
Syntax:
CALL sub-prog-name USING variable-1, variable-2

2) Call By Content
In call by content, if the values of variables in the subprogram(called program) are changed then their new values will not be reflected in the main program(calling program).

Syntax:
CALL sub-prog-name USING
BY CONTENT variable-1, BY CONTENT variable-2

Types of Call

There are two types of call:
1. Static Call
2. Dynamic Call

1) Static call
  • Static call arises when the compiler compiles a program with NODYNAM option.
  • In static call, all the modules are available in the main memory. Hence, the processing becomes fast and it does not require additional time to load.
  • Static call statement specifies the subprogram name as literal, i.e in quotes.
2) Dynamic call
  • Dynamic call arises when the compiler compiles a program with DYNAM option.
  • In dynamic call, calling and called program loads are available separately.
  • The size of the module is small because it occupies less memory while loading into the main memory.
  • In dynamic call, processing is a bit slower than the static call because loading and unloading of main and subprograms in memory decreases the speed of execution.