Subroutines in COBOL Language

Subroutines in COBOL Language

Subroutines are essential building blocks in COBOL programming, enabling code modularity, reusability, and maintainability. In this b

log post, we’ll explore subroutines in COBOL, discuss their benefits, and provide examples to illustrate their usage.

Understanding Subroutines in COBOL Language

Subroutines, also known as procedures or paragraphs in COBOL, are self-contained blocks of code that perform specific tasks. They are designed to be reusable and can be called from multiple parts of a program, reducing redundancy and making code more manageable. Subroutines can accept parameters (arguments) and return results, enhancing their flexibility.

In COBOL, you define a subroutine using the PERFORM or CALL statement, followed by the name of the subroutine, and optionally, parameters. A subroutine is terminated with a GOBACK statement.

Example: Subroutine Definition

Here’s how you define a simple COBOL subroutine:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SubroutineExample.

       DATA DIVISION.

       WORKING-STORAGE SECTION.
       01 Counter          PIC 9(3) VALUE 0.

       PROCEDURE DIVISION.

       Main-Program.
           DISPLAY "Main program begins."
           PERFORM My-Subroutine
           DISPLAY "Main program ends."
           STOP RUN.

       My-Subroutine.
           ADD 1 TO Counter
           DISPLAY "Inside subroutine: Counter = " Counter
           GOBACK.

In this example:

  • We define a subroutine called My-Subroutine that increments the Counter variable.
  • The PERFORM My-Subroutine statement is used to call the subroutine from the Main-Program.
  • The GOBACK statement indicates the end of the subroutine.

Benefits of Subroutines in COBOL

  1. Code Reusability: Subroutines can be called from various parts of a program or even different programs, reducing code duplication.
  2. Modularity: Subroutines break down a program into smaller, manageable units, making it easier to develop, maintain, and understand.
  3. Maintainability: Changes or updates to a subroutine only need to be made once, affecting all instances where it is called.
  4. Parameter Passing: Subroutines can accept parameters, allowing data to be passed into and out of the subroutine, enhancing their flexibility.
  5. Encapsulation: Subroutines encapsulate specific logic or functionality, providing a clear structure to a program.

Example: Subroutine with Parameters

Now, let’s enhance our example by passing a parameter to the subroutine:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SubroutineWithParamExample.

       DATA DIVISION.

       WORKING-STORAGE SECTION.
       01 Counter          PIC 9(3) VALUE 0.

       PROCEDURE DIVISION.

       Main-Program.
           DISPLAY "Main program begins."
           PERFORM My-Subroutine WITH 5
           DISPLAY "Main program ends."
           STOP RUN.

       My-Subroutine USING Param-Value.
           ADD Param-Value TO Counter
           DISPLAY "Inside subroutine: Counter = " Counter
           GOBACK.

In this modified example, we pass a parameter (Param-Value) to the subroutine, and the subroutine increments Counter by the value passed.


Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading