COBOL - Sub-Program
As we discussed in the previous chapter. Call statement is used to transfer the control from one program to another program. The program that contains the CALL statement is the Calling Program and the program being called is known as the Called Program.
Calling Program Struture:
- CALL statement should be added.
Called Program Struture:
- Linkage section must be defined in the called program. It consists of data elements passed in the program. The data items should not have Value clause. PIC clause must be compatible with the variables passed through the calling program.
- Procedure division using has a list of variables passed from the calling program and the order must be same as mentioned in the Call verb.
- Exit program statement is used in the called program to transfer the control back. It must be the last statement in the called program.
Example 1: Call By Reference(If the values of variables in the called program are modified, then their new values will reflect in the calling program. If BY clause is not specified, then variables are always passed by reference.)
CALL Program:
IDENTIFICATION DIVISION.
PROGRAM-ID. COBLMAIN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STUDENT-ID PIC 9(4) VALUE 1000.
01 WS-STUDENT-NAME PIC A(15) VALUE 'Tim'.
PROCEDURE DIVISION.
CALL 'UTIL' USING WS-STUDENT-ID, WS-STUDENT-NAME.
DISPLAY 'Student Id : ' WS-STUDENT-ID
DISPLAY 'Student Name : ' WS-STUDENT-NAME
STOP RUN.
|
Called Program:
IDENTIFICATION DIVISION.
PROGRAM-ID. COBLUTIL.
DATA DIVISION.
LINKAGE SECTION.
01 LS-STUDENT-ID PIC 9(4).
01 LS-STUDENT-NAME PIC A(15).
PROCEDURE DIVISION USING LS-STUDENT-ID, LS-STUDENT-NAME.
DISPLAY 'In Called Program'.
MOVE 1111 TO LS-STUDENT-ID.
EXIT PROGRAM. |
When you compile and execute the above program, it produces the following result −
SYSOUT Result:
In Called Program
Student Id : 1111
Student Name : Tim |
Example 2: Call By Content(If the values of variables in the called program are modified, then their new values will not reflect in the calling program.)
CALL Program:
IDENTIFICATION DIVISION.
PROGRAM-ID. COBLMAIN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STUDENT-ID PIC 9(4) VALUE 1000.
01 WS-STUDENT-NAME PIC A(15) VALUE 'Tim'.
PROCEDURE DIVISION.
CALL 'UTIL' USING BY CONTENT WS-STUDENT-ID, BY CONTENT WS-STUDENT-NAME.
DISPLAY 'Student Id : ' WS-STUDENT-ID
DISPLAY 'Student Name : ' WS-STUDENT-NAME
STOP RUN. |
Called Program:
IDENTIFICATION DIVISION.
PROGRAM-ID. COBLUTIL.
DATA DIVISION.
LINKAGE SECTION.
01 LS-STUDENT-ID PIC 9(4).
01 LS-STUDENT-NAME PIC A(15).
PROCEDURE DIVISION USING LS-STUDENT-ID, LS-STUDENT-NAME.
DISPLAY 'In Called Program'.
MOVE 1111 TO LS-STUDENT-ID.
EXIT PROGRAM. |
When you compile and execute the above program, it produces the following result −
SYSOUT Result:
In Called Program
Student Id : 1000
Student Name : Tim |
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!