Subroutines in COBOL Language
Subroutines are essential building blocks in COBOL programming, enabling code modularity, reusability, and maintainability. In this blog post, we’ll explore subroutines in COBOL, discuss their benefits, and provide examp
les 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-Subroutinethat increments theCountervariable. - The
PERFORM My-Subroutinestatement is used to call the subroutine from theMain-Program. - The
GOBACKstatement indicates the end of the subroutine.
Benefits of Subroutines in COBOL
- Code Reusability: Subroutines can be called from various parts of a program or even different programs, reducing code duplication.
- Modularity: Subroutines break down a program into smaller, manageable units, making it easier to develop, maintain, and understand.
- Maintainability: Changes or updates to a subroutine only need to be made once, affecting all instances where it is called.
- Parameter Passing: Subroutines can accept parameters, allowing data to be passed into and out of the subroutine, enhancing their flexibility.
- 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 - Embedded Systems & VLSI Lab
Subscribe to get the latest posts sent to your email.



