Modular programming is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules. Each module contains everything necessary to execute only one aspect of the desired functionality. This approach enhances code readability, maintainability, and reusability. In COBOL, modular programming is achieved through the use of subprograms and the CALL statement.

Key Concepts

  1. Modularity: Breaking down a program into smaller, manageable, and reusable pieces.
  2. Subprograms: Independent program units that can be called from other parts of the program.
  3. CALL Statement: Used to invoke a subprogram.
  4. Parameter Passing: Mechanism to pass data between the main program and subprograms.

Benefits of Modular Programming

  • Improved Readability: Smaller, well-defined modules are easier to understand.
  • Easier Maintenance: Changes in one module do not affect others.
  • Reusability: Modules can be reused across different programs.
  • Parallel Development: Different modules can be developed simultaneously by different teams.

Creating and Using Subprograms

Structure of a Subprogram

A subprogram in COBOL is typically a separate COBOL program that can be called from a main program. It has its own IDENTIFICATION DIVISION, ENVIRONMENT DIVISION, DATA DIVISION, and PROCEDURE DIVISION.

Example Subprogram

       IDENTIFICATION DIVISION.
       PROGRAM-ID. CALCULATE-TAX.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 TAX-RATE       PIC 9V99 VALUE 0.05.
       LINKAGE SECTION.
       01 INCOME         PIC 9(5)V99.
       01 TAX-AMOUNT     PIC 9(5)V99.
       PROCEDURE DIVISION USING INCOME TAX-AMOUNT.
           COMPUTE TAX-AMOUNT = INCOME * TAX-RATE
           EXIT PROGRAM.
       END PROGRAM CALCULATE-TAX.

Calling a Subprogram

In the main program, you use the CALL statement to invoke the subprogram.

Example Main Program

       IDENTIFICATION DIVISION.
       PROGRAM-ID. MAIN-PROGRAM.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 INCOME         PIC 9(5)V99 VALUE 1000.00.
       01 TAX-AMOUNT     PIC 9(5)V99.
       PROCEDURE DIVISION.
           CALL 'CALCULATE-TAX' USING INCOME TAX-AMOUNT
           DISPLAY 'Income: ' INCOME
           DISPLAY 'Tax Amount: ' TAX-AMOUNT
           STOP RUN.

Explanation

  • Subprogram: CALCULATE-TAX computes the tax amount based on a fixed tax rate.
  • Main Program: MAIN-PROGRAM calls CALCULATE-TAX and passes INCOME and TAX-AMOUNT as parameters.

Parameter Passing

Parameters can be passed by reference or by value. In COBOL, parameters are typically passed by reference, meaning the subprogram can modify the original data.

Example of Parameter Passing

       CALL 'SUBPROGRAM' USING BY REFERENCE VAR1 BY VALUE VAR2.
  • BY REFERENCE: The subprogram can modify VAR1.
  • BY VALUE: The subprogram receives a copy of VAR2 and cannot modify the original variable.

Practical Exercise

Task

Create a COBOL program that calculates the area of a rectangle using a subprogram.

Main Program

       IDENTIFICATION DIVISION.
       PROGRAM-ID. MAIN-PROGRAM.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 LENGTH         PIC 9(3)V99 VALUE 10.00.
       01 WIDTH          PIC 9(3)V99 VALUE 5.00.
       01 AREA           PIC 9(5)V99.
       PROCEDURE DIVISION.
           CALL 'CALCULATE-AREA' USING LENGTH WIDTH AREA
           DISPLAY 'Length: ' LENGTH
           DISPLAY 'Width: ' WIDTH
           DISPLAY 'Area: ' AREA
           STOP RUN.

Subprogram

       IDENTIFICATION DIVISION.
       PROGRAM-ID. CALCULATE-AREA.
       DATA DIVISION.
       LINKAGE SECTION.
       01 LENGTH         PIC 9(3)V99.
       01 WIDTH          PIC 9(3)V99.
       01 AREA           PIC 9(5)V99.
       PROCEDURE DIVISION USING LENGTH WIDTH AREA.
           COMPUTE AREA = LENGTH * WIDTH
           EXIT PROGRAM.
       END PROGRAM CALCULATE-AREA.

Solution Explanation

  • Main Program: Initializes LENGTH and WIDTH, calls CALCULATE-AREA, and displays the results.
  • Subprogram: CALCULATE-AREA computes the area of the rectangle and returns the result.

Common Mistakes and Tips

  • Incorrect Parameter Order: Ensure the order of parameters in the CALL statement matches the order in the subprogram's USING clause.
  • Uninitialized Variables: Always initialize variables before passing them to subprograms.
  • Data Type Mismatch: Ensure the data types of parameters match between the main program and subprogram.

Conclusion

Modular programming in COBOL enhances code organization, readability, and maintainability. By breaking down a program into subprograms, you can manage complexity more effectively and promote code reuse. Practice creating and using subprograms to become proficient in modular programming techniques in COBOL.

© Copyright 2024. All rights reserved