COBOL CALL OPERATIONS

There are certain times when we have to deal with multiple programs at same time, and sometimes, we have to access multiple variables from called program or sub program to the main program or the calling program. COBOL provides us with a special feature called call operation, with this, we can access sub program from the main program and can directly use variables defined there.

CALL OR CONTROL PASSING STATEMENTS

There are different types of control passing statements within a single program or inter calling within different programs

  1. GOTO => Command generally used to go to a specific part or specific paragraph.

EXAMPLE:

PARA – 1.

MOVE 10 TO A.

DISPLAY ‘INITIALLY A IS: ‘A.

GOTO PARA – 2.

DISPLAY ‘ CONTROL CAME FROM PARA – 2 . A IS NOW : ‘ A.

STOP RUN.

PARA – 2.

MOVE 5 TO A .

THE OUTPUT WILL BE

INITIALLY A IS : 10

CONTROL CAME FROM PARA – 2 . A IS NOW : 5                                

  • EXIT PROGRAM => It is mostly used in an external subroutine (i.e., a CALLED program), it gives the control back to the CALLED program.

EXAMPLE=>

Main Program : MAIN – PGM

CALL SUB – PGM

DISPLAY “ HII : “

Sub Program : SUB – PGM

EXIT PROGRAM

.EXIT PROGRAM will ensure that we exit from the subprogram, and the control goes back to the calling program. Hence in this example, we can use the EXIT PROGRAM in the subprogram to go back to the main program.

  • GO BACK => If this command is used in a CALLED program, then it works similar to EXIT PROGRAM. But if it is commanded in a CALLING program, then it will work similar to STOP RUN. In CALLING program it’s always better to go for STOP RUN rather than using GO BACK.

EXAMPLE=>

Main Program: MAIN – PGM

CALL SUB – PGM

DISPLAY “ HII : “

Sub Program : SUB – PGM

GO BACK

. . . . GO BACK will pass the control back to the calling program. Hence you can use GO BACK to go in the subprogram in the given example.

  • STOP RUN => It is used to stop or end the execution of the program. It closes all the files used in the program. This should always be commanded in a CALLING program but never in a CALLED program.

UNDERSTANDING CALL IN COBOL

A program can call other program or multiple programs to perform multiple tasks. In COBOL, the program which calls other programs is called a CALLING PROGRAM or the MAIN PROGRAM, while the program which is being called is known as CALLED PROGRAM or the SUB PROGRAM.

There are two types of calls:

  1. STATIC CALL =>

SYNTAX =>

CALL ‘ SUB – PROGRAM ‘

CALL ‘ SSUB – PGM ‘

CALL ‘ SSUB – PGM ‘ USING PRM -1 , PRM -2 , . . . . . ( call using arguments )

Both programs are compiled as a separate PDS member but are linked and edited together at the same time.

  • DYNAMIC CALL

SYNTAX =>

CALL ‘ WS – VARIABLE ‘ ( ws – variable refers to the name of the program)

CALL ‘ WS – SUB – PGM ‘

CALL ‘ WS – SUB – PGM ‘ USING PRM – 1 , PRM – 2 , . . . . . ( WS – SUB – PGM is a working storage variable )

We can define WS – SUB – PGM as

01 WS – SUB – PGM PIC X (08) VALUE ‘ DSUB – PGM ‘

OR

ACCEPT WS – SUB –PGM

In dynamic called, programs are compiled separately, and unlike static, they have different or separate PDS. Thus, dynamic call is comparatively faster because while calling it only the required programs are changed rest are left undisturbed.          

CALLING A SUB PROGRAM

  1. Without passing any value (simple CALL ): Calling can be simple, i.e., without any value being passed to the sub-program or bypassing arguments.
  • CALL BY REFERENCE – To refer the same memory location for parameters in calling or called program. All the value and parameter will be reflected in the main program or CALLING PROGRAM that we will change in the subprogram.

SYNTAX =>

USING BY REFERENCE identifier1, identifier2 , . . . . .

  • CALL BY CONTENT – To send the copy of the content of the parameter, but not the memory location. Any changes to the subprogram will not reflect in the main program / CALLING PROGRAM.

SYNATX =>

USING BY CONTENT identifier1 , identifier2 , . . . . .

  • CALL BY VALUE – In this, only limited values can be sent to the called program rest is similar to call by content. But, only one-byte alphanumeric value or an integer can be passed in call by value method. Therefore, it’s not usually preferred amongst the others.

SYNATX =>

USING BY VALUE identifier1 , identifier2 , . . . . .

————————————————————————————————————————–

SAMPLE PROGRAM –

This is a demo program to learn and understand the concept of program calling using call by reference, and how to assign values from different subprograms.

IDENTIFICATION DIVISION.

PROGRAM-ID. CALLED.

DATA DIVISION.

WORKING-STORAGE SECTION.

01 WS-STORE-PROD-TABLE.

   05 WS-STORE-ID OCCURS 3 TIMES.

     10 WS-PRODUCT OCCURS 5 TIMES PIC 9(08).

01 WS-TIME PIC 9(08).

01 I PIC 9(01).

01 J PIC 9(01).

LINKAGE SECTION.

01 LS-SEQ PIC 9(08).

PROCEDURE DIVISION USING LS-SEQ.

DISPLAY “I AM EXAMPLE OF TWO DIMENSIONAL ARRAY”.

PERFORM PRODUCT-PARA VARYING I FROM 1 BY 1 UNTIL I > 3

AFTER J FROM 1 BY 1 UNTIL J > 5.

EXIT PROGRAM.

PRODUCT-PARA.

   MOVE LS-SEQ TO WS-PRODUCT(I, J).

   DISPLAY “PRODUCT” I “,” J

          WS-PRODUCT(I, J).

   ADD 1 TO LS-SEQ.

(THIS IS THE CALLED PROGRAM OR THE SUB PROGRAM WHICH WILL BE CALLED BY THE MAIN PROGRAM)

IDENTIFICATION DIVISION.

PROGRAM-ID. CALLING.

ENVIRONMENT DIVISION.

DATA DIVISION.

WORKING-STORAGE SECTION.

01 WS-SEQ PIC 9(08).

PROCEDURE DIVISION.

MAIN-PARA.

ACCEPT WS-SEQ FROM TIME.

DISPLAY “I AM IN CALLING PROGRAM”

DISPLAY “TIME: ” WS-SEQ.

CALL ‘CALLEDP’ USING WS-SEQ.

DISPLAY ‘Hello, world’.

STOP RUN.

( THIS IS THE MAIN OR THE CALLING PROGRAM THROUGH WHICH CALLED IS CALLED )

THE OUTPUT WILL BE

I AM IN CALLING PROGRAM

TIME: 20194829

I AM EXAMPLE OF TWO DIMENSIONAL ARRAY

PRODUCT 1 , 20194829

PRODUCT 1 , 20194830

PRODUCT 1 , 20194831

PRODUCT 1 , 20194832

PRODUCT 1 , 20194833

PRODUCT 2 , 20194834

PRODUCT 2 , 20194835

PRODUCT 2 , 20194836

PRODUCT 2 , 20194837

PRODUCT 2 , 20194838

PRODUCT 3 , 20194839

PRODUCT 3 , 20194840

PRODUCT 3 , 20194841

PRODUCT 3 , 20194842

PRODUCT 3 , 20194843

TIME : AFTER 20194844

Here in this program, we have assigned a few variables with different values and later manipulated the output using different call properties to clarify the different concepts related to CALLED / SUBPROGRAM AND MAIN / CALLING PROGRAM. With this program and output, you must be understood that how convenient and easy program calling is. In the end, the final value is changed due to change in the memory location caused by call by reference method.

SAMPLE PROGRAM – CALL BY VALUE / CONTENT

This is a demo program to learn and understand the concept of program calling by call by value method and how to assign values from a different subprogram and later by displaying the result.

IDENTIFICATION DIVISION.

PROGRAM-ID. CALLED.

DATA DIVISION.

WORKING-STORAGE SECTION.

01 WS-STORE-PROD-TABLE.

   05 WS-STORE-ID OCCURS 3 TIMES.

     10 WS-PRODUCT OCCURS 5 TIMES PIC 9(08).

01 WS-TIME PIC 9(08).

01 I PIC 9(01).

01 J PIC 9(01).

LINKAGE SECTION.

01 LS-SEQ PIC 9(08).

PROCEDURE DIVISION USING LS-SEQ.

DISPLAY “I AM EXAMPLE OF TWO DIMENSIONAL ARRAY”.

PERFORM PRODUCT-PARA VARYING I FROM 1 BY 1 UNTIL I > 3

AFTER J FROM 1 BY 1 UNTIL J > 5.

EXIT PROGRAM.

PRODUCT-PARA.

   MOVE LS-SEQ TO WS-PRODUCT(I, J).

   DISPLAY “PRODUCT” I “,” J

          WS-PRODUCT(I, J).

   ADD 1 TO LS-SEQ.

(THIS IS THE CALLED PROGRAM OR THE SUB PROGRAM WHICH WILL BE CALLED BY THE MAIN PROGRAM)

IDENTIFICATION DIVISION.

PROGRAM-ID. CALLING.

ENVIRONMENT DIVISION.

DATA DIVISION.

WORKING-STORAGE SECTION.

01 WS-SEQ PIC 9(08).

PROCEDURE DIVISION.

MAIN-PARA.

ACCEPT WS-SEQ FROM TIME.

DISPLAY “I AM IN CALLING PROGRAM”

DISPLAY “TIME: ” WS-SEQ.

CALL ‘CALLEDP’ USING BY CONTENT WS-SEQ.

DISPLAY ‘Hello, world’.

STOP RUN.

( THIS IS THE MAIN OR THE CALLING PROGRAM THROUGH WHICH CALLED IS CALLED )

THE OUTPUT WILL BE

I AM IN CALLING PROGRAM

TIME: 22195702

I AM EXAMPLE OF TWO DIMENSIONAL ARRAY

PRODUCT 1 , 1 22195702

PRODUCT 1 , 2 22195703

PRODUCT 1 , 3 22195704

PRODUCT 1 , 4 22195705

PRODUCT 1 , 5 22195706

PRODUCT 2 , 1 22195707

PRODUCT 2 , 2 22195708

PRODUCT 2 , 3 22195709

PRODUCT 2 , 4 22195710

PRODUCT 2 , 5 22195711

PRODUCT 3 , 1 22195712

PRODUCT 3 , 2 22195713

PRODUCT 3 , 3 22195714

PRODUCT 3 , 4 22195715

PRODUCT 3 , 5 22195716

TIME : AFTER 22195702

Here in this program, we have assigned a few variables with different values and later manipulated the output using different call properties to clarify the different concepts related to CALLED / SUBPROGRAM AND MAIN / CALLING PROGRAM. In the end the final result remains the same as was in the starting, due to call by content, the final value doesn’t change.

Pin It on Pinterest

Share This