The CALL statement in COBOL is used to invoke subprograms or external programs from within a COBOL program. This allows for modular programming, where complex tasks can be broken down into smaller, manageable subprograms. This section will cover the syntax, usage, and examples of the CALL statement.

Key Concepts

  1. Subprograms: Independent COBOL programs that can be called from other COBOL programs.
  2. CALL Statement: The command used to invoke a subprogram.
  3. Linkage Section: A section in the subprogram where parameters passed from the calling program are defined.
  4. Parameter Passing: Mechanism to pass data between the calling program and the subprogram.

Syntax

The basic syntax of the CALL statement is as follows:

CALL 'subprogram-name' USING parameter-1, parameter-2, ...
  • subprogram-name: The name of the subprogram to be called.
  • parameter-1, parameter-2, ...: The parameters to be passed to the subprogram.

Example

Let's look at a simple example to understand how the CALL statement works.

Main Program (Calling Program)

IDENTIFICATION DIVISION.
PROGRAM-ID. MainProgram.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(2) VALUE 10.
01 WS-NUM2 PIC 9(2) VALUE 20.
01 WS-RESULT PIC 9(2).

PROCEDURE DIVISION.
    DISPLAY 'Calling SubProgram...'.
    CALL 'SubProgram' USING WS-NUM1, WS-NUM2, WS-RESULT.
    DISPLAY 'Result from SubProgram: ' WS-RESULT.
    STOP RUN.

Subprogram (Called Program)

IDENTIFICATION DIVISION.
PROGRAM-ID. SubProgram.

DATA DIVISION.
LINKAGE SECTION.
01 LK-NUM1 PIC 9(2).
01 LK-NUM2 PIC 9(2).
01 LK-RESULT PIC 9(2).

PROCEDURE DIVISION USING LK-NUM1, LK-NUM2, LK-RESULT.
    COMPUTE LK-RESULT = LK-NUM1 + LK-NUM2.
    EXIT PROGRAM.

Explanation

  1. Main Program:

    • Defines three variables: WS-NUM1, WS-NUM2, and WS-RESULT.
    • Calls the subprogram SubProgram using the CALL statement and passes the three variables.
    • Displays the result returned by the subprogram.
  2. Subprogram:

    • Defines the LINKAGE SECTION to receive the parameters from the calling program.
    • Uses the USING clause in the PROCEDURE DIVISION to specify the parameters.
    • Computes the sum of LK-NUM1 and LK-NUM2 and stores the result in LK-RESULT.
    • Returns control to the calling program using the EXIT PROGRAM statement.

Practical Exercise

Exercise

Write a main program that calls a subprogram to multiply two numbers and return the result.

  1. Main Program:

    • Define two variables for the numbers to be multiplied.
    • Define a variable to store the result.
    • Call the subprogram and pass the variables.
    • Display the result.
  2. Subprogram:

    • Define the LINKAGE SECTION to receive the parameters.
    • Multiply the two numbers and store the result.
    • Return the result to the calling program.

Solution

Main Program

IDENTIFICATION DIVISION.
PROGRAM-ID. MainProgram.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(2) VALUE 5.
01 WS-NUM2 PIC 9(2) VALUE 4.
01 WS-RESULT PIC 9(2).

PROCEDURE DIVISION.
    DISPLAY 'Calling SubProgram to multiply numbers...'.
    CALL 'MultiplySubProgram' USING WS-NUM1, WS-NUM2, WS-RESULT.
    DISPLAY 'Result from SubProgram: ' WS-RESULT.
    STOP RUN.

Subprogram

IDENTIFICATION DIVISION.
PROGRAM-ID. MultiplySubProgram.

DATA DIVISION.
LINKAGE SECTION.
01 LK-NUM1 PIC 9(2).
01 LK-NUM2 PIC 9(2).
01 LK-RESULT PIC 9(2).

PROCEDURE DIVISION USING LK-NUM1, LK-NUM2, LK-RESULT.
    COMPUTE LK-RESULT = LK-NUM1 * LK-NUM2.
    EXIT PROGRAM.

Common Mistakes and Tips

  1. Incorrect Parameter Order: Ensure that the order of parameters in the CALL statement matches the order in the USING clause of the subprogram.
  2. Data Type Mismatch: Ensure that the data types of the parameters in the calling program and the subprogram match.
  3. Uninitialized Variables: Initialize variables before passing them to the subprogram to avoid unexpected results.

Conclusion

The CALL statement is a powerful feature in COBOL that promotes modular programming by allowing the invocation of subprograms. By understanding the syntax and usage of the CALL statement, you can create more organized and maintainable COBOL programs. In the next topic, we will explore how to pass parameters between programs in more detail.

© Copyright 2024. All rights reserved