Introduction
COBOL (Common Business-Oriented Language) has been a workhorse of the programming world for decades, especially in business and financial applications. One of the key features that makes COBOL a versatile and efficient language is its CALL statement. The CALL statement allows programmers to create modular and organized programs by invoking subprograms, also known as subroutines or procedures, from within a main COBOL program. In this article, we will delve into the intricacies of the CALL statement, exploring its various use cases and scenarios, and provide real-world examples to illustrate its functionality.
Understanding the CALL Statement
The CALL statement in COBOL is used to transfer control from the main program to a specified subprogram. This subprogram can be another COBOL program, a COBOL-DB2 program, or any other type of program that is written in a compatible language. The CALL statement facilitates the creation of modular code, as it enables the reuse of existing subprograms across different parts of an application.
Basic Syntax of the CALL Statement
The basic syntax of the CALL statement is as follows:
CALL 'SUBPROGRAM-NAME'<code>
Where 'SUBPROGRAM-NAME'
is the name of the subprogram being called. The subprogram can be located within the same source code file or in a separate file altogether.
Scenarios of Calling Subprograms
Let’s explore various scenarios of using the CALL statement to invoke subprograms from a main COBOL program.
- Calling an Internal Subroutine: In this scenario, we define a subroutine within the same COBOL program and invoke it using the CALL statement. This approach is useful for breaking down a complex task into smaller, more manageable units of code.
Example:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINPROGRAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM1 PIC 9(3) VALUE 100.
01 NUM2 PIC 9(3) VALUE 200.
PROCEDURE DIVISION.
PERFORM CALCULATE-SUM
DISPLAY "Sum: " NUM1
STOP RUN.
CALCULATE-SUM.
ADD NUM1 TO NUM2.
- Calling an External Subprogram: In this scenario, we have a separate COBOL program that performs a specific task. The main program calls this external program using the CALL statement to leverage its functionality.
Example:
Main Program (MAINPROGRAM.CBL
):
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINPROGRAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AMOUNT PIC 9(5) VALUE 5000.
PROCEDURE DIVISION.
CALL 'CALCSUB'
DISPLAY "New Amount: " AMOUNT
STOP RUN.
Subprogram (CALCSUB.CBL
):
IDENTIFICATION DIVISION.
PROGRAM-ID. CALCSUB.
DATA DIVISION.
LINKAGE SECTION.
01 AMT-IN PIC 9(5).
PROCEDURE DIVISION USING AMT-IN.
MULTIPLY AMT-IN BY 1.1 GIVING AMT-IN.
EXIT PROGRAM.
- Calling a COBOL-DB2 Program: COBOL is often integrated with database systems like IBM DB2. You can call a COBOL-DB2 program to perform database operations, such as fetching records, updating data, or executing SQL queries.
Example:
Main Program (MAINPROGRAM.CBL
):
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINPROGRAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 EMPLOYEE-NAME PIC X(30).
01 EMPLOYEE-ID PIC 9(6) VALUE 123456.
PROCEDURE DIVISION.
CALL 'DB2SUB' USING EMPLOYEE-ID EMPLOYEE-NAME
DISPLAY "Employee: " EMPLOYEE-NAME
STOP RUN.
COBOL-DB2 Subprogram (DB2SUB.CBL
):
IDENTIFICATION DIVISION.
PROGRAM-ID. DB2SUB.
DATA DIVISION.
LINKAGE SECTION.
01 EMP-ID-IN PIC 9(6).
01 EMP-NAME-OUT PIC X(30).
PROCEDURE DIVISION USING EMP-ID-IN EMP-NAME-OUT.
EXEC SQL
SELECT EMP_NAME INTO :EMP-NAME-OUT
FROM EMPLOYEES
WHERE EMP_ID = :EMP-ID-IN;
EXIT PROGRAM.
- Calling a Non-COBOL Program: COBOL can also call programs written in other languages, provided there is interoperability. In this scenario, let’s consider calling a C program from COBOL.
Example:
Main COBOL Program (MAINPROGRAM.CBL
):
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINPROGRAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM PIC 9(3) VALUE 50.
PROCEDURE DIVISION.
CALL 'CPROG' USING NUM
DISPLAY "Squared: " NUM
STOP RUN.
C Program (CPROG.C
):
#include <stdio.h>
void cprog_(int *num) {
*num = (*num) * (*num);
}
Conclusion
The CALL statement is a powerful feature in COBOL that enables modular programming and code reuse. It allows COBOL programs to call internal and external subprograms, including COBOL-DB2 programs and programs written in other languages. By harnessing the capabilities of the CALL statement, developers can create more organized, maintainable, and efficient applications. Whether it’s breaking down complex tasks, integrating with databases, or collaborating with other programming languages, the CALL statement empowers COBOL programmers to build robust and versatile solutions for a wide range of business and technical requirements.