In COBOL, passing parameters between programs or subprograms is a fundamental concept that allows for modular and reusable code. This section will cover the basics of passing parameters, including the different methods and best practices.
Key Concepts
- CALL Statement: Used to invoke a subprogram.
- Linkage Section: Defines the parameters that are passed to and from a subprogram.
- USING Clause: Specifies the parameters to be passed.
- BY REFERENCE vs. BY CONTENT: Methods of passing parameters.
CALL Statement
The CALL
statement is used to invoke a subprogram. The syntax is as follows:
Example
In this example, the main program calls the subprogram CALCULATE-TAX
and passes two parameters: TAX-RATE
and GROSS-INCOME
.
Linkage Section
The LINKAGE SECTION
is used in the subprogram to define the parameters that are passed from the main program. The PROCEDURE DIVISION
header must also include a USING
clause to specify these parameters.
Example
LINKAGE SECTION. 01 TAX-RATE PIC 9(3)V99. 01 GROSS-INCOME PIC 9(7)V99. PROCEDURE DIVISION USING TAX-RATE GROSS-INCOME.
BY REFERENCE vs. BY CONTENT
BY REFERENCE
When parameters are passed BY REFERENCE
, the subprogram can modify the original data in the calling program. This is the default method.
BY CONTENT
When parameters are passed BY CONTENT
, the subprogram receives a copy of the data, and any modifications do not affect the original data in the calling program.
Example
In this example, TAX-RATE
is passed by reference, and GROSS-INCOME
is passed by content.
Practical Example
Main Program
IDENTIFICATION DIVISION. PROGRAM-ID. MAIN-PROGRAM. DATA DIVISION. WORKING-STORAGE SECTION. 01 TAX-RATE PIC 9(3)V99 VALUE 0.15. 01 GROSS-INCOME PIC 9(7)V99 VALUE 50000.00. 01 NET-INCOME PIC 9(7)V99. PROCEDURE DIVISION. CALL 'CALCULATE-TAX' USING TAX-RATE GROSS-INCOME NET-INCOME. DISPLAY 'Net Income: ' NET-INCOME. STOP RUN.
Subprogram
IDENTIFICATION DIVISION. PROGRAM-ID. CALCULATE-TAX. DATA DIVISION. LINKAGE SECTION. 01 TAX-RATE PIC 9(3)V99. 01 GROSS-INCOME PIC 9(7)V99. 01 NET-INCOME PIC 9(7)V99. PROCEDURE DIVISION USING TAX-RATE GROSS-INCOME NET-INCOME. COMPUTE NET-INCOME = GROSS-INCOME - (GROSS-INCOME * TAX-RATE). EXIT PROGRAM.
In this example, the main program calls the subprogram CALCULATE-TAX
, passing TAX-RATE
, GROSS-INCOME
, and NET-INCOME
. The subprogram calculates the net income and returns the result to the main program.
Exercises
Exercise 1
Task: Write a main program and a subprogram where the main program passes two numbers to the subprogram, and the subprogram returns their sum.
Solution:
Main Program
IDENTIFICATION DIVISION. PROGRAM-ID. MAIN-PROGRAM. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUMBER-1 PIC 9(5) VALUE 10. 01 NUMBER-2 PIC 9(5) VALUE 20. 01 SUM PIC 9(5). PROCEDURE DIVISION. CALL 'ADD-NUMBERS' USING NUMBER-1 NUMBER-2 SUM. DISPLAY 'Sum: ' SUM. STOP RUN.
Subprogram
IDENTIFICATION DIVISION. PROGRAM-ID. ADD-NUMBERS. DATA DIVISION. LINKAGE SECTION. 01 NUMBER-1 PIC 9(5). 01 NUMBER-2 PIC 9(5). 01 SUM PIC 9(5). PROCEDURE DIVISION USING NUMBER-1 NUMBER-2 SUM. COMPUTE SUM = NUMBER-1 + NUMBER-2. EXIT PROGRAM.
Exercise 2
Task: Modify the previous exercise to pass the numbers by content and the sum by reference.
Solution:
Main Program
IDENTIFICATION DIVISION. PROGRAM-ID. MAIN-PROGRAM. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUMBER-1 PIC 9(5) VALUE 10. 01 NUMBER-2 PIC 9(5) VALUE 20. 01 SUM PIC 9(5). PROCEDURE DIVISION. CALL 'ADD-NUMBERS' USING BY CONTENT NUMBER-1 BY CONTENT NUMBER-2 BY REFERENCE SUM. DISPLAY 'Sum: ' SUM. STOP RUN.
Subprogram
IDENTIFICATION DIVISION. PROGRAM-ID. ADD-NUMBERS. DATA DIVISION. LINKAGE SECTION. 01 NUMBER-1 PIC 9(5). 01 NUMBER-2 PIC 9(5). 01 SUM PIC 9(5). PROCEDURE DIVISION USING NUMBER-1 NUMBER-2 SUM. COMPUTE SUM = NUMBER-1 + NUMBER-2. EXIT PROGRAM.
Common Mistakes and Tips
- Mismatch in Data Types: Ensure that the data types of the parameters in the main program and subprogram match.
- Incorrect USING Clause: Verify that the
USING
clause in thePROCEDURE DIVISION
header of the subprogram matches the parameters defined in theLINKAGE SECTION
. - Uninitialized Variables: Initialize variables in the main program before passing them to the subprogram to avoid unexpected results.
Conclusion
Passing parameters in COBOL is a powerful feature that promotes modularity and code reuse. By understanding the CALL
statement, LINKAGE SECTION
, and the differences between BY REFERENCE
and BY CONTENT
, you can effectively manage data flow between programs and subprograms. Practice with the provided exercises to reinforce your understanding and avoid common pitfalls.