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
- Subprograms: Independent COBOL programs that can be called from other COBOL programs.
- CALL Statement: The command used to invoke a subprogram.
- Linkage Section: A section in the subprogram where parameters passed from the calling program are defined.
- Parameter Passing: Mechanism to pass data between the calling program and the subprogram.
Syntax
The basic syntax of the CALL
statement is as follows:
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
-
Main Program:
- Defines three variables:
WS-NUM1
,WS-NUM2
, andWS-RESULT
. - Calls the subprogram
SubProgram
using theCALL
statement and passes the three variables. - Displays the result returned by the subprogram.
- Defines three variables:
-
Subprogram:
- Defines the
LINKAGE SECTION
to receive the parameters from the calling program. - Uses the
USING
clause in thePROCEDURE DIVISION
to specify the parameters. - Computes the sum of
LK-NUM1
andLK-NUM2
and stores the result inLK-RESULT
. - Returns control to the calling program using the
EXIT PROGRAM
statement.
- Defines the
Practical Exercise
Exercise
Write a main program that calls a subprogram to multiply two numbers and return the result.
-
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.
-
Subprogram:
- Define the
LINKAGE SECTION
to receive the parameters. - Multiply the two numbers and store the result.
- Return the result to the calling program.
- Define the
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
- Incorrect Parameter Order: Ensure that the order of parameters in the
CALL
statement matches the order in theUSING
clause of the subprogram. - Data Type Mismatch: Ensure that the data types of the parameters in the calling program and the subprogram match.
- 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.