Sunday, November 20, 2011

CALL statement


When a specific functionality need to be performed in more than one program, it is best to write them separately and call them into each program. Sub Programs can be written in any programming language. They are typically written in a language best suited to the specific task required and thus provide greater flexibility.

Main Program Changes:
          CALL statement is used for executing the sub-program from the main program. A sample of CALL statement is given below:
          CALL ‘PGM2’ USING BY REFERENCE WS-VAR1, BY CONTENT WS-VAR2.
PGM2 is called here. WS-VAR1 and WS-VAR2 are working storage items.
WS-VAR1 is passed by reference. WS-VAR2 is passed by Content. BY REFERENCE is default in COBOL and need not be coded. BY CONTENT LENGTH phrase permits the length of data item to be passed to a called program.

Sub-Program Changes:
WS-VAR1 and WS-VAR2 are working storage items of main program.
As we have already mentioned, the linkage section is used for accessing external elements. As these working storage items are owned by main program, to access them in the sub-program, we need to define them in the linkage section.

LINKAGE SECTION.
01 LINKAGE SECTION.
               05 LK-VAR1 PIC 9(04).
     05 LK-VAR2 PIC 9(04).

In addition to define them in linkage section, the procedure division should be coded with these data items for address-ability.

PROCEDURE DIVISION USING LK-VAR1,LK-VAR2

There is a one-one correspondence between passed elements and received elements (Call using, linkage and procedure division using) BY POSITION. This implies that the name of the identifiers in the called and calling program need not be the same (WS-VAR1 & LK-VAR1) but the number of elements and picture clause should be same.

The last statement of your sub-program should be EXIT PROGRAM. This returns the control back to main program. GOBACK can also be coded instead of EXIT PROGRAM but not STOP RUN. EXIT PROGRAM should be the only statement in a paragraph in COBOL74 whereas it can be coded along with other statements in a paragraph in COBOL85.

PROGRAM-ID. <Program-name> IS INITIAL PROGRAM.
          If IS INITIAL PROGRAM is coded along with program-id of sub program, then the program will be in initial stage every time it is called (COBOL85 feature).
Alternatively CANCEL issued after CALL, will set the sub-program to initial state. 

If the sub program is modified then it needs to be recompiled. The need for main program recompilation is decided by the compiler option used for the main program. If the DYNAM compiler is used, then there is no need to recompile the main program. The modified subroutine will be in effect during the run. NODYNAM is default that expects the main program recompilation.

No comments:

Post a Comment