COBOL CALLing SUB Programs, by reference & Content, Examples
Call is the COBOL Verb to transfer control to another program from a program. Simple CALL, call by reference, and call by content are the different types. CALL verb is used within the procedure division to transfer the control to the subprogram and once the subprogram processing is over, control comes back to the main program’s next executable statement. But here the subprogram should use the EXIT PROGRAM or GO BACK statement as the end of the program statement. If a STOP RUN is used, the control goes back to the operating system without coming back to the main program after the sub-module execution.
Along with passing control to the next module, data can also be sent using a CALL Statement and the changed values in the subprogram can also be viewed in the main program. So it allows bi-directional data flow.
Below are the different ways a CALL verb used.
SIMPLE CALL
IN this method, there is no data exchange between the main program and subprogram.
Example : CALL WW-SUB-PGM
Here the working storage variable WW-SUB-PGM contains the Sub program name.
But if the requirement is to pass some data to the subprogram then the above method is not suitable. So COBOL has provided a Phrase called USING along with the CALL verb. With this, required data can be sent to the sub-module.
Example:
CALL WW-SUB-PGM USING WK-IN-AREA
Here WK-IN-AREA is the working storage item and it contains the data that is to be passed.
Here the main program has sent some data to the submodule so to receive or hold that data in the subprogram, there should be some extra coding required in the sub module as mentioned below.
SUB PROGRAM Considerations:
- The linkage section should be coded with the data item declaration to receive the data.
- PROCEDURE division should have a USING phrase.
Example code:
Main Program:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINP1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WW-SEND-AREA.
05 WW-NAME PIC X(10) VALUE 'MAHENDER'.
05 WW-AMOUNT PIC 9(06) VALUE 123456.
*
01 WK-SUB-PROG PIC X(08) VALUE 'SBPGM1'.
*
PROCEDURE DIVISION.
DISPLAY '********MAIN PGM START*********'
DISPLAY 'NAME PASSED IS -->' WW-NAME
DISPLAY 'AMOUNT PASSED IS -->' WW-AMOUNT
DISPLAY '********************************'
CALL WK-SUB-PROG USING WW-SEND-AREA
DISPLAY 'NAME AFTER RETURN -->' WW-NAME
DISPLAY 'AMOUNT AFTER RETURN-->' WW-AMOUNT
DISPLAY '*******MAIN PGM END*************'
STOP RUN.
SUB Program:
IDENTIFICATION DIVISION.
PROGRAM-ID. SBPGM1.
ENVIRONMENT DIVISION.
DATA DIVISION.
LINKAGE SECTION.
01 LS-LINK-AREA.
05 LS-NAME-IN PIC X(10).
05 LS-AMOUNT-IN PIC 9(06).
*
PROCEDURE DIVISION USING LS-LINK-AREA.
DISPLAY '********SUB PGM START***********'
DISPLAY 'NAME RECIEVED IS -->' LS-NAME-IN
DISPLAY 'AMOUNT RECIEVED IS -->' LS-AMOUNT-IN
*--> CHANGE THE AMOUNT =
COMPUTE LS-AMOUNT-IN = LS-AMOUNT-IN - 100000
DISPLAY 'CHANGED AMOUNT IS -->' LS-AMOUNT-IN
DISPLAY '********SUB PGM END*************'
GOBACK.
Spool
MAIN PGM START*
NAME PASSED IS -->MAHENDER
AMOUNT PASSED IS -->123456
SUB PGM START***
NAME RECIEVED IS -->MAHENDER
AMOUNT RECIEVED IS -->123456
CHANGED AMOUNT IS -->123000
**SUB PGM END*
NAME AFTER RETURN -->MAHENDER
AMOUNT AFTER RETURN-->123000
MAIN PGM END
Observations:
- Observe that in the main program, the data that is being sent is declared in working storage only.
- In the subprogram, the same data structure should be defined in the Linkage section to receive the passed data.
- Here only single variable has been sent but multiple variables can also be sent.
- Observe the procedure division, it has a USING phrase.
As mentioned data can be passed to the next module with USING Phrase, but what happens if the data that is sent gets modified in the subprogram? Does that change also visible in the main program after the control comes back to the main module?
The answer is, it depends on the way that the call is made.
In the above example, the modified data can be seen in the main module.
Call BY Reference and CALL by CONTENT are the different ways of calling a subprogram from the main program.
CALL BY REFERENCE:
In this method, the changes made in the submodule are also effected in the main module. That means any changes to the data in the sub program are visible in the main program as well.This is the default one and if nothing is specified by default it would be in effect.
In this method, the address of the variable is passed to the next module so that submodule accesses & processes the variable on its storage address, so any changes made are to the original copy of the data. That is the reason changes made in any module are visible to the other one.
In the above example, nothing is specified so CALL BY REFERENCE is in effect and the subprogram can modify the data which also gets affected in the main program.
Example:
CALL WW-SUB-PGM USING BY REFERENCE WW-IN-AREA
CALL BY CONTENT:
With this way of calling, the changes are not visible in the main program and the changes are specific to that sub module only.
In this method, contents of the variable are sent and not the address. So whatever the changes made in the sub module are not on the original copy of the data so the changes are not visible in the main one.
Example:
CALL WW-SUB-PGM USING BY CONTENT WW-IN-AREA
Passing Multiple data items:
Only one data item has been sent in the above example, but multiple data items can also be passed to the sub module.
Main Program:
WORKING-STORAGE SECTION. 01 WW-SEND-1 PIC X(10) VALUE 'MAHENDER'. 01 WW-SEND-2 PIC X(20) VALUE 'HYDERABAD INDIA'. 01 WW-SEND-3 PIC X(30) VALUE 'ABCD 12345 CREDIT'.
PROCEDURE DIVISION.
CALL WW-SUB-PROG USING WW-SEND-1, WW-SEND-2, WW-SEND-3.
SUB Program:
LINKAGE SECTION. 01 LS-RECV-1 PIC X(10). 01 LS-RECV-2 PIC X(20). 01 LS-RECV-3 PIC X(30).
PROCEDURE DIVISION USING LS-RECV-1,LS-RECV-2,LS-RECV-3.
To send multiple data items CALL.. USING Phrase in main program should contain all those data items and the Sub program should contain them in the linkage section and in the procedure Using phrase.