In this section, we will explore the concept of subprograms in COBOL, which are essential for creating modular and maintainable code. Subprograms allow you to break down complex tasks into smaller, manageable pieces, making your programs easier to understand, test, and debug.
Key Concepts
- Subprograms: Independent sections of code that perform specific tasks and can be called from other parts of the program.
- CALL Statement: Used to invoke a subprogram.
- Linkage Section: Used to define parameters that are passed between the main program and the subprogram.
- Parameter Passing: Mechanism to send data to and receive data from subprograms.
Structure of a COBOL Subprogram
A COBOL subprogram has a specific structure that includes the following divisions:
- IDENTIFICATION DIVISION: Contains the program ID.
- ENVIRONMENT DIVISION: Defines the environment in which the program runs.
- DATA DIVISION: Contains the WORKING-STORAGE SECTION and LINKAGE SECTION.
- PROCEDURE DIVISION: Contains the executable code.
Example of a Simple Subprogram
Below is an example of a simple COBOL subprogram that calculates the square of a number.
IDENTIFICATION DIVISION. PROGRAM-ID. CalculateSquare. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUMBER PIC 9(4). 01 WS-RESULT PIC 9(8). LINKAGE SECTION. 01 LK-NUMBER PIC 9(4). 01 LK-RESULT PIC 9(8). PROCEDURE DIVISION USING LK-NUMBER LK-RESULT. MOVE LK-NUMBER TO WS-NUMBER COMPUTE WS-RESULT = WS-NUMBER * WS-NUMBER MOVE WS-RESULT TO LK-RESULT EXIT PROGRAM.
Explanation
- IDENTIFICATION DIVISION: Specifies the name of the subprogram (
CalculateSquare
). - LINKAGE SECTION: Defines the parameters (
LK-NUMBER
andLK-RESULT
) that will be passed to and from the subprogram. - PROCEDURE DIVISION: Contains the logic to calculate the square of the number. The
USING
clause specifies the parameters that the subprogram will use.
Calling a Subprogram
To call the CalculateSquare
subprogram from a main program, you use the CALL
statement.
Example of a Main Program Calling a Subprogram
IDENTIFICATION DIVISION. PROGRAM-ID. MainProgram. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUMBER PIC 9(4) VALUE 5. 01 WS-RESULT PIC 9(8). PROCEDURE DIVISION. CALL 'CalculateSquare' USING WS-NUMBER WS-RESULT DISPLAY 'The square of ' WS-NUMBER ' is ' WS-RESULT STOP RUN.
Explanation
- CALL Statement: The
CALL 'CalculateSquare' USING WS-NUMBER WS-RESULT
statement invokes theCalculateSquare
subprogram, passingWS-NUMBER
andWS-RESULT
as parameters. - DISPLAY Statement: Displays the result returned by the subprogram.
Practical Exercise
Exercise
Write a COBOL subprogram named CalculateCube
that calculates the cube of a number. Then, write a main program that calls this subprogram and displays the result.
Solution
Subprogram: CalculateCube
IDENTIFICATION DIVISION. PROGRAM-ID. CalculateCube. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUMBER PIC 9(4). 01 WS-RESULT PIC 9(8). LINKAGE SECTION. 01 LK-NUMBER PIC 9(4). 01 LK-RESULT PIC 9(8). PROCEDURE DIVISION USING LK-NUMBER LK-RESULT. MOVE LK-NUMBER TO WS-NUMBER COMPUTE WS-RESULT = WS-NUMBER * WS-NUMBER * WS-NUMBER MOVE WS-RESULT TO LK-RESULT EXIT PROGRAM.
Main Program
IDENTIFICATION DIVISION. PROGRAM-ID. MainProgram. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUMBER PIC 9(4) VALUE 3. 01 WS-RESULT PIC 9(8). PROCEDURE DIVISION. CALL 'CalculateCube' USING WS-NUMBER WS-RESULT DISPLAY 'The cube of ' WS-NUMBER ' is ' WS-RESULT STOP RUN.
Explanation
- The
CalculateCube
subprogram calculates the cube of a number and returns the result. - The main program calls the
CalculateCube
subprogram and displays the result.
Summary
In this section, we covered the basics of subprograms in COBOL, including their structure, how to call them using the CALL
statement, and how to pass parameters. Subprograms are a powerful tool for creating modular and maintainable code. In the next section, we will delve deeper into the CALL
statement and explore different ways to pass parameters between programs.