Wednesday, February 17, 2010

14. Call statement

Structured programs should consist of a series of independent modules that are executed from the main module.
When programs are properly structured :
1. Each module may be written, compiled, and perhaps even tested independently.
2. The modules may be written in different stages, in atop-down manner. They may even be coded by different programmers.
3. If a specific module needs to be modified, the entire logical flow should still function properly without the need for extensive revision to other parts of the program.

Modules within a program can be viewed as subroutines that are called or executed from the main module. But a program may also CALL or reference independent subprograms stored in a library that are entirely separate from the main program itself. The main program that references or calls a subprogram is referred to as the calling program. The subprogram that is linked and executed within the main program is referred to as the called program.
The called program would need to be compiled so that it may be called when needed.
When the CALL is performed, data is passed from the calling to the called program (if the calling program has assigned values to fields used in the called program). The entire called program is executed, data is passed from the called program back to the calling program, and control return to the calling program.


Called Program Requirements
PROGRAM-ID.
The literal used in the CALL statement of the main program to extract a subprogram or routine from a library and execute it must be identical to the called program’s PROGRAM-ID. Note that the literal is enclosed in quotation marks when used in the CALL statement.

LINKAGE SECTION
A LINKAGE SECTION must be defined in the called program for identifying those items that (1) will be passed to the called program from the calling program and (2) passed back from the called program to the calling program. The LINKAGE SECTION of the called program, then, describes all items to be passed between the two programs.
The LINKAGE SECTION, if used, is coded after the FILE and WORKING-STORAGE SECTIONs of the called program. This section is similar to WORKING-STORAGE except that VALUE clauses from initializing fields are not permitted in the LINKAGE SECTION.

EXIT PROGRAM
The last executed statement in the called program must be the EXIT PROGRAM. It signals the computer to return control back to the calling program. With COBOL 74, EXIT PROGRAM must be the only statement in the last paragraph.

Calling Program Requirements
PROCEDURE DIVISION USING
The identifiers specified in the USING clause in the PROCEDURE DIVISION entry include all fields defined in the LINKAGE SECTION; these identifiers will be passed from one program to the other. They are passed to and from corresponding identifiers in the CALL … USING of the main program.
The USING clause of the CALL statement is required if the subprogram performs any operations in which data is to be passed from one program to another. The CALL … USING identifies fields in the main or calling program that will be either passed to the called program before it is executed, or passed back to the calling program after the called program has been executed.

Sample Program

Calling Program : MAIN.CBL
Eg 14.1a:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MY-NAME PIC X(20).
PROCEDURE DIVISION.
MAIN.
DISPLAY "ENTER NAME ".
ACCEPT MY-NAME.
CALL "DISP" USING MY-NAME.
STOP RUN.

Called Program : DISP.CBL
Eg 14.1b:
IDENTIFICATION DIVISION.
PROGRAM-ID. DISP.
ENVIRONMENT DIVISION.
DATA DIVISION.
LINKAGE SECTION.
01 NM PIC X(20).
PROCEDURE DIVISION USING NM.
MAIN.
DISPLAY "HELLO " NM.
EXIT PROGRAM.

Back to COBOL Index

No comments: