Microsoft KB Archive/58216

Sample Program Using ANSI 1985 Nested Programs in COBOL

PSS ID Number: Q58216 Article last modified on 02-07-1990

i3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: The program below demonstrates the use of ANSI 1985 nested programs and the INITIAL clause. Programs are “nested” when an IDENTIFICATION DIVISION … END PROGRAM block is located within another IDENTIFICATION DIVISION … END PROGRAM block in the same source file. This information applies to Microsoft COBOL Versions 3.00 and 3.00a for MS-DOS and MS OS/2.

More Information: To use nested programs, both the ANS85 and NESTCALL directives must be specified. Normally, data in a nested program is only initialized once (when first CALLed). The INITIAL clause on the PROGRAM-ID line, however, causes the data to be reinitialized each time the routine is called. Each nested program must begin with an IDENTIFICATION DIVISION and end with an END PROGRAM line. There are some restrictions when CALLing nested programs. Any program, including nested programs, can CALL any separately compiled program. A CALL cannot be made, however, to a program nested in another program, unless IS COMMON is specified on the PROGRAM-ID line. For example, consider a program structured as follows: OUTER-PROGRAM FIRST-INNER-PROGRAM SECOND-LEVEL-PROGRAM-1 END SECOND-LEVEL-PROGRAM-1 SECOND-LEVEL-PROGRAM-2 END SECOND-LEVEL-PROGRAM-2 END FIRST-INNER-PROGRAM SECOND-INNER-PROGRAM END SECOND-INNER-PROGRAM END OUTER-PROGRAM SECOND-LEVEL-PROGRAM-1 can only be called by FIRST-INNER-PROGRAM; it cannot be called by either SECOND-LEVEL-PROGRAM-2 or OUTER-PROGRAM. SECOND-INNER-PROGRAM can only be called by OUTER-PROGRAM. In both cases, if IS COMMON is specified on the PROGRAM-ID lines of SECOND-LEVEL-PROGRAM-1 and SECOND-INNER-PROGRAM, they can be called from any of the other programs. The following program shows a nested program and demonstrates the effect of the INITIAL clause: $SET ANS85 $SET NESTCALL IDENTIFICATION DIVISION. PROGRAM-ID. PROG-ONE. PROCEDURE DIVISION. PERFORM 2 times CALL “PROG-TWO” CALL “PROG-TWO” CALL “PROG-THREE” CALL “PROG-THREE” end-perform. STOP RUN. IDENTIFICATION DIVISION. PROGRAM-ID. PROG-TWO, IS INITIAL. DATA DIVISION. WORKING-STORAGE SECTION. 01 WORK-J PIC 9(5) VALUE 9006. PROCEDURE DIVISION. DISPLAY “In two:” WORK-J. MOVE 0 TO WORK-J. END PROGRAM PROG-TWO. IDENTIFICATION DIVISION. PROGRAM-ID. PROG-THREE. DATA DIVISION. WORKING-STORAGE SECTION. 01 WORK-J PIC 9(5) VALUE 1234. PROCEDURE DIVISION. DISPLAY “In three:” WORK-J. MOVE 0 TO WORK-J. END PROGRAM PROG-THREE. END PROGRAM PROG-ONE. The output is as follows: In two: 09006 In two: 09006 In three: 01234 In three: 00000 In two: 09006 In two: 09006 In three: 00000 In three: 00000

Copyright Microsoft Corporation 1990.