In this topic, we described about the CALL Statement with detailed example.
If a task required to be performed from more than one program, it suggested to separate the code to eliminate the code redundancy. The separated code used to create a program to call it from the other programs where ever it is required.
CALL statement transfers the control from one object program to another object program to complete a task. In this, the program referred as object module of the program.
The program containing the CALL statement is the calling program or main program. The program identified in the CALL statement is the called program or subprogram. Calling programs or calling program can contain CALL statements as many as required.
There are two types of CALLs based on how the subprogram name specified in CALL statement.
- Static Call
- Dynamic call
Static call: -
The main program load module is dependent on the subprogram changes. Called program (subprogram) added to the calling program (main program) load module. In static call, the subprogram name specified with single quotes in CALL statement.
- Compiler option should be NODYNAM for static calls.
- If subprogram is modified, the subprogram and main program needs to be compiled to update the load module to pick up the latest changes.
- Size of the load module is big because the main, subprogram load modules are combined.
- Processing is fast because once the load module loads to main memory, both programs gets loaded.
Now a days, the static call is not suggested.
Dynamic call: -
The main program, subprogram is independent. In dynamic call, the subprogram name specified with single quotes in CALL statement.
- WS-PGM-NAME is the literal or working-storage data item and the program name passed during the runtime before the CALL.
- Compiler option should be DYNAM for dynamic calls.
- Calling Program (main program) and Called program (sub program) loads are created separately.
- Size of the load modules are small because the main, subprogram load modules are stored separately.
- Processing is slow because the load modules should load to main memory separately. Loading and unloading decreases speed of execution.
- If sub program modified, no need to recompile the main program again.
- literal-1 must be an alphanumeric literal.
- identifier-1 must be an alphanumeric, alphabetic or numeric data item declared with USAGE DISPLAY.
- literal-1 or identifier-1 contents must specify the program-name of the called subprogram.
Specifies arguments are passed to the target program.
If either CALL statement or PROCEDURE DIVISION has the USING phase, the other should have the USING phase. Otherwise it is not required to specify.
The USING phase must be identical with CALL statement or PROCEDURE DIVISION.
The sequence of the operands in the CALL statement USING phrase and the called subprogram's procedure division USING phrase should have correspondence between them. This correspondence is positional.
The parameter values in USING phase of CALL statement are made available to the called program.The parameters declaration in both called and calling programs should be same.
Accessing level of passing values in the calling program can be decided based on how they sent to the calling program.
There are three ways of passing parameters from main program to sub program -
- BY CONTENT
- BY REFERENCE
- BY VALUE
BY REFERENCE is assumed if not specified any from BY CONTENT, BY REFERENCE or BY VALUE phrase.
BY REFERENCE phrase
If BY REFERENCE phrase is specified, the corresponding data items in the calling program occupies the same storage area as the data items in the called program.
BY REFERENCE phrase can be specified for single data item.
Can be any data item of any level in data division.
If it is defined in the linkage section or file section, must provide addressability for identifier-2 prior to invocation of the CALL statement.
A file-name for a QSAM file.
ADDRESS OF identifier-2
identifier-2 must be a level-01 or level-77 item defined in the linkage section.
Indicates that no argument is passed.
BY CONTENT phrase
If the BY CONTENT phrase is specified, the called program can't change the value of te parameters as referenced in the CALL statement's USING phrase.
However, called program can change the value of the data items referenced in the called program's procedure division internally where the changes are local to the called program.
Changes to the parameter in the called program do not affect the corresponding parameter value in the calling program.
Can be any data item of any level in the data division.
If it is defined in the linkage section or file section, must provide addressability for identifier-3 prior to invocation of the CALL statement.
Can be an alphanumeric literal, a figurative constant (except ALL literal or NULL/NULLS).
ADDRESS OF identifier-3
identifier-3 must be a data item of any level except 66 or 88 defined in the linkage section or the working-storage section.
Indicates that no argument is passed.
BY VALUE phrase
If the BY VALUE phrase is specified, the value of the argument is passed.
The called program can modify the parameter that corresponds to the BY VALUE argument.
However, such changes do not affect the argument because the called program has access to a temporary copy of the sending data item.
Although BY VALUE arguments are primarily intended for communication with non-COBOL programs (such as C) and can also be used for COBOL-to-COBOL invocations.
Must be an elementary data item in the data division.
It must be a Binary (USAGE BINARY, COMP, COMP-4 or COMP-5), Floating point (USAGE COMP-1 or COMP-2) and One single-byte alphanumeric character (such as PIC X or PIC A).
ADDRESS OF identifier-4
identifier-4 must be a data item of any level except 66 or 88 defined in the linkage section or the working-storage section.
Can be a numeric literal, a figurative constant ZERO, a one-character alphanumeric literal, a symbolic character, a single-byte figurative constant (SPACE, QUOTE, HIGH-VALUE or LOW-VALUE).
ZERO is treated as a numeric value; a full word binary zero is passed.
Can be any data item defined in the data division.
The return value of the called program is stored into identifier-5.
If the RETURNING phrase specified on a CALL to a COBOL subprogram, called subprogram must specify the RETURNING phrase on its procedure division header and identifier-5 and the corresponding procedure division RETURNING identifier must have the same PICTURE, USAGE, SIGN, SYNCHRONIZE, JUSTIFIED, and BLANK WHEN ZERO clauses.
On entry to the called program, the initial state of the PROCEDURE DIVISION RETURNING data item has an undefined and unpredictable value. RETURNING DATA ITEM must initialize the PROCEDURE DIVISION RETURNING data item in the called program before referring its value.
If an EXCEPTION or OVERFLOW occurs, identifier-5 is not changed.
ON EXCEPTION phrase
An exception condition occurs when the called subprogram can't available to run.
If the ON EXCEPTION phrase is specified, control is transferred to imperative-statement-1 and then execution continues according to the rules for each statement specified in imperative-statement-1.
NOT ON EXCEPTION phrase
If an exception condition does not occur, control is transferred to the called program.
Once the called program execution completed, control transferred to imperative-statement-2 and then execution continues according to the rules for each statement specified in imperative-statement-2.
ON OVERFLOW phrase
The ON OVERFLOW phrase has the same effect as the ON EXCEPTION phrase.
The END-CALL used to end the scope of the CALL statement.
END-CALL is not required when CALL statement ended with period.