COBOL Call Statement

COBOL Accept Statement COBOL Compute Statement

Advertisements


When a particular task needs to be performed in more than one program, it is better to write a subprogram.

The need of CALL statement is to call the subprogram to perform the task.

CALL statement transfers the control from one object program to another object to complete a particular task.

The program which had a CALL statement in it called as CALLING program/Main program.

The program name in CALL statement called as CALLED program/Sub program.

A program can contain as many CALLs required and no restriction on it. In other words, CALLING program can CALL as many subprograms required.

CALLED may not have the CALL statement to call another program.

The CALL statement creates a tree structure if programs create a chain of CALLs and control will always return to the CALLING program.

Let’s discuss how the CALL in Main program and sub program.


Main program:


Main program / CALLING program which had the CALL statement.

There are two types of CALLs based on how the sub program is calling from Main program.


  1. Static Call
  2. Dynamic call

Based on the how the program is calling in CALL statement, the load creation of sub program will be decided.

Let’s discuss in details about both types of calls


1. Static call:

Static call can be decided based on the syntax of the CALL statement.


CALL 'sub-program-name'

Compiler option for Static call is NODYNAM

Calling Program (main program) and Called program (sub program) loads are available in load module of main program.

It occupies more memory while loading to main memory hence the size of load module is big.

Processing will be fast because all the module load available on main memory and no need of additional time needed to load.

Static call statement specifies the sub program name as literal (in quotes).

Based on the above declaration, linker will copy the object code of sub program to main program during linking.

If sub program modified, then main program needs to recompile to link again.


2. Dynamic call:

Dynamic call can be decided based on the syntax of the CALL statement.


CALL WS-PGM-NAME

In the above syntax, WS-PGM-NAME is the working storage field name and the called module name should to pass to the above working-storage field before CALL.

Compiler Option for Dynamic call is DYNAM.

Calling Program (main program) and Called program (sub program) loads are available separately.

It occupies less memory while loading to main memory hence the size of load module is small.

Processing will be a bit slow because main program calling Subprogram then main program will be replaced with subprogram in the memory and once sub program completes execution again main program will be loaded. Loading and unloading will decrease speed of execution.

If sub program modified, no need to recompile the main program again.


Sub Program (CALLED program):


Sub program/ CALLED program which name had associated with CALL statement.

As we discussed earlier LINKAGE SECTION uses to receive the external data. Sub program uses the LINKAGE SECTION to declare variables in Sub program to receive the data form main program.

The name of the variable in sub program need not be the same as main program.

Once the variables declared in LINKAGE SECTION, the same can be coded in PROCEDURE DIVISION USAGE Clause.

The order of the parameters should be same as the main program CALL.

Sub program should not be coded with STOP RUN which will not return the control to main program.

Sub program can be coded with EXIT PROGRAM or GOBACK to return the control to the main program once the sub program execution completed.

Sub program syntax would be same for both static and dynamic call.


Syntax:

PROCEDURE DIVISION [USING [BY VALUE] identifier1,..,identifierN].

In the above syntax, USING clause is optional. For the CALL without any parameters from Main program doesn’t need any USING clause.

BY VALUE clause should be needed when the CALL in the main program had BY VALUE Clause.

BY REFERENCE, BY CONTENT was not needed in Sub program even though they are coded in Main program during the CALL.

There might be possibility that sub program can be called more than once from the Main program and for every CALL the program will not initialize.

To overcome this, COBOL introduced a special feature INITIAL in Identification to initialize the sub program for every CALL.


Syntax:

PROGRAM-ID. Prog-name IS INITIAL.

Note:  If program is coded to call a program statically, the some compilers can convert it to the dynamic form of CALL regardless of the user options and compile the program always with DYNAM.


Types of CALLs based on the parameters passing from Main program to Sub program.


There are two types of CALLs based on the parameters passing from Main program to Sub program.


  1. Simple CALL without parameters.
  2. CALLING with parameters.

Let’s discuss in details about both types of CALLs one by one.


Simple CALL without parameters:


This case, Sub program doesn’t need any inputs from main program to perform the task coded in Sub-program.

The main advantage of this is, Sub program not required any data from main program and can be easily called from any program.

Syntax:

CALL ‘Sub-program-name’/ WS-PGM-NAME

Simple CALL can be applied to both static and dynamic CALL types.

The syntax for each type of CALLs like below.


Static CALL syntax: 	CALL ‘Sub-program-name’

Dynamic CALL syntax: 	CALL WS-PGM-NAME	

Calling subprogram with parameters:


In this case, Sub program need inputs from main program to perform the task coded in Sub-program.

The Main program is responsible for taking care of data sending to sub program. If the main program failed to send the data to sub program, there might be a chance of abend occurs in sub program.

CALLING subprogram with parameters applicable for both Static and dynamic type of calls as well. The syntax for the both like below.


Syntax:

Static CALL: 
	
	CALL ‘Sub-program-name’ USING identifier1, identifier2,…,identifierN.
	

Dynamic CALL: 	

	CALL WS-PGM-NAME	 USING identifier1, identifier2,…,identifierN.
	

The USING clause plays a key role in passing the parameters from main program to sub program. Let’s discuss in details about the USING clause.


USING Clause:


USING clause specifies about the arguments passing to sub program from main.

USING clause also used along with PROCEDURE DIVISION in the subprogram to receive parameters passing from Main.

The parameters passing from main program and parameters received in sub program should be same. i.e. the number of parameters with USING clause in both main program and sub program should be same.

The order of the parameters in both Main program and sub program should be same. i.e. The order of receiving parameters with USING should be the same as how they are in Main program.

The declaration of parameters should be same in both main and subprograms.

The only difference is USING clause in main program come up with CALL statement and in sub program it is come up with PROCEDURE DIVISION.

There are three types in passing parameters from main program to sub program.


  1. BY REFERENCE
  2. BY CONTENT
  3. BY VALUE

Let’s discuss about each option one by one.


1. BY REFERENCE:

BY REFERENCE clause specified during CALL, same memory will be used for parameters both main program and sub program.

In this case, Main program shares the address of parameters to the sub program.

Any changes to the value in the sub program will reflect in main program also because of same memory sharing between the programs.

The changes done in sub program (called program) is global and visible to main program (calling program) also.

BY REFERENCE applicable for both static and dynamic CALLs.


Syntax:

Static CALL:	

	CALL ‘Sub-program-name’ USING BY REFERENCE identifier1,…,identifierN.

Dynamic CALL: 
	
	CALL WS-PGM-NAME	 USING BY REFERENCE identifier1,…,identifierN.

2. BY CONTENT:

BY CONTENT clause specified during CALL, main program sends only content of parameters.

In this case, Main program shares the content of parameters to the sub program.

Any changes to the content in the sub program will not reflect in main program.

The changes done in sub program (called program) is internal to sub program and not visible to main program (calling program).

BY CONTENT applicable for both static and dynamic CALLs.


Syntax:

Static CALL: 

	CALL ‘Sub-program-name’ USING BY CONTENT identifier1,…,identifierN.

Dynamic CALL: 
	
	CALL WS-PGM-NAME	 USING BY CONTENT identifier1,…,identifierN.

3. BY VALUE:

BY VALUE clause specified during CALL, main program sends only Value of parameters.

In this case, Main program shares the value of parameters to the sub program.

Any changes to the value in the sub program will not reflect in main program.

The changes done in sub program (called program) is internal to sub program and not visible to main program (calling program).

BY VALUE mainly designed for calling other than COBOL program. It can be used for COBOL-TO-COBOL but BY VALUE clause should be coded in both palces along with USING clause.

BY VALUE applicable for both static and dynamic CALLs.


Syntax:

Static CALL: 

	CALL ‘Sub-program-name’ USING BY VALUE identifier1,…,identifierN.

Dynamic CALL: 
	
	CALL WS-PGM-NAME	 USING BY VALUE identifier1,…,identifierN.

COBOL Accept Statement COBOL Compute Statement

Advertisements