Exercises — Chapter 24: Nested Programs

Exercise 24.1: Your First Nested Program (Beginner)

Objective: Create a simple nested program structure and observe compilation and execution.

Requirements:

  1. Write an outer program MATHTEST that: - Defines two numeric values in WORKING-STORAGE - Calls a nested program ADD-NUMS to add them - Calls a nested program MUL-NUMS to multiply them - Displays both results

  2. ADD-NUMS (nested): - Receives two numbers and a result field via LINKAGE SECTION - Adds the numbers and stores in result - Returns via GOBACK

  3. MUL-NUMS (nested): - Same interface as ADD-NUMS - Multiplies instead of adding

  4. Ensure proper END PROGRAM markers for all programs.

  5. Compile as a single unit and run.

Questions: - How many source files did you need? - How many object files were produced by compilation? - Can ADD-NUMS call MUL-NUMS? Why or why not?


Exercise 24.2: The COMMON Attribute (Beginner)

Objective: Practice using COMMON to enable sibling program calls.

Requirements:

  1. Extend Exercise 24.1 by adding: - A nested DISPLAY-RESULT program that formats and displays a number - Mark DISPLAY-RESULT as COMMON

  2. Modify ADD-NUMS and MUL-NUMS to call DISPLAY-RESULT to display their results (instead of having the outer program display them).

  3. Verify that: - MATHTEST can call DISPLAY-RESULT (parent calling COMMON child) - ADD-NUMS can call DISPLAY-RESULT (sibling calling COMMON sibling) - MUL-NUMS can call DISPLAY-RESULT (sibling calling COMMON sibling)

  4. What happens if you remove the COMMON attribute from DISPLAY-RESULT? Try it and document the compiler error.


Exercise 24.3: GLOBAL Data Sharing (Intermediate)

Objective: Practice using GLOBAL to share data between nested programs.

Requirements:

  1. Write an outer program COUNTER-DEMO with: - A GLOBAL counter: WS-CALL-COUNT PIC 9(4) VALUE 0 IS GLOBAL - A GLOBAL accumulator: WS-TOTAL PIC S9(9)V99 COMP-3 VALUE 0 IS GLOBAL

  2. Create three nested programs (all COMMON): - ADD-VALUE: Receives a numeric value, adds it to WS-TOTAL, increments WS-CALL-COUNT - GET-AVERAGE: Computes WS-TOTAL / WS-CALL-COUNT and returns it via LINKAGE - RESET-COUNTERS: Sets both GLOBAL items back to their initial values

  3. The outer program should: - Call ADD-VALUE five times with values 10, 20, 30, 40, 50 - Display the GLOBAL counter and total after each call - Call GET-AVERAGE and display the result - Call RESET-COUNTERS - Verify both GLOBAL items are back to initial values

  4. Bonus: Add a nested program ADD-AND-SHOW that calls both ADD-VALUE and DISPLAY-RESULT (from Exercise 24.2) to demonstrate sibling-to-sibling calls.


Exercise 24.4: GLOBAL File Sharing (Intermediate)

Objective: Use GLOBAL to share a file between nested programs.

Requirements:

  1. Write an outer program LOG-DEMO that: - Defines a sequential output file as GLOBAL - Opens the file, calls nested programs, closes the file

  2. Create nested programs: - LOG-INFO (COMMON): Writes an INFO-level message with timestamp - LOG-WARN (COMMON): Writes a WARNING-level message with timestamp - LOG-ERROR (COMMON): Writes an ERROR-level message with timestamp

  3. Each logging program should: - Format the message as: YYYY-MM-DD HH:MM:SS [LEVEL] message - Write directly to the GLOBAL file (no parameter passing for the file) - Increment a GLOBAL line counter

  4. The outer program should: - Write several messages at different levels - Display the total line count at the end


Exercise 24.5: Scope Rules Exploration (Intermediate)

Objective: Understand and demonstrate GLOBAL shadowing and scope behavior.

Tasks:

  1. Create the following nested structure: SCOPE-TEST (outer) ├── LEVEL-1A │ └── LEVEL-2A └── LEVEL-1B

  2. In SCOPE-TEST, define: - WS-SHARED PIC X(10) VALUE 'OUTER' IS GLOBAL - WS-PRIVATE PIC X(10) VALUE 'PRIVATE'

  3. In LEVEL-1A, define: - WS-SHARED PIC X(10) VALUE 'LEVEL-1A' (shadows the GLOBAL)

  4. In LEVEL-2A, define NO local WS-SHARED (should see LEVEL-1A's? Or OUTER's?)

  5. In LEVEL-1B, define NO local WS-SHARED

  6. Have each program DISPLAY WS-SHARED and document: - What does SCOPE-TEST display? - What does LEVEL-1A display? - What does LEVEL-2A display? (Tricky!) - What does LEVEL-1B display?

  7. Key question: When LEVEL-2A accesses WS-SHARED, does it see OUTER's GLOBAL or LEVEL-1A's local shadow? Test and explain.


Exercise 24.6: Hybrid Architecture Design (Advanced)

Objective: Design a system that appropriately uses both nested and external subprograms.

Scenario: A student grade processing system that: - Reads student records from a file - Calculates GPA - Determines honors status - Generates a report - Logs processing statistics

Design task:

  1. Identify which components should be: - External subprograms (reusable across multiple callers) - Nested programs (internal utilities specific to this system)

  2. For each component, justify your choice with reference to the decision framework in Section 24.9.

  3. Decide which data items should be GLOBAL and which should be passed as parameters.

  4. Implement the system with at least: - One external subprogram (compile separately) - Three nested programs (at least one COMMON) - One GLOBAL data item

  5. Write a brief design document (comments in the source code) explaining your architectural decisions.


Exercise 24.7: Nested Program Debugging Challenge (Advanced)

Objective: Find and fix bugs in a nested program structure.

The following program has five bugs. Find them all.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. BUGGY.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-TOTAL    PIC 9(5) IS GLOBAL VALUE 0.
       01  WS-RESULT   PIC 9(5).
       01  WS-RC       PIC S9(4) COMP.

       PROCEDURE DIVISION.
           CALL 'ADDER' USING 10 WS-RC
           CALL 'ADDER' USING 20 WS-RC
           CALL 'SHOWER'
           DISPLAY 'Result: ' WS-RESULT
           STOP RUN.

           IDENTIFICATION DIVISION.
           PROGRAM-ID. ADDER.
           LINKAGE SECTION.
           01  LS-VALUE    PIC 9(5).
           01  LS-RC       PIC S9(4) COMP.
           PROCEDURE DIVISION USING LS-VALUE LS-RC.
               ADD LS-VALUE TO WS-TOTAL
               MOVE 0 TO LS-RC
               GOBACK.
           END PROGRAM ADDER.

           IDENTIFICATION DIVISION.
           PROGRAM-ID. SHOWER.
           PROCEDURE DIVISION.
               CALL 'FORMATTER'
               STOP RUN.
           END PROGRAM SHOWER.

           IDENTIFICATION DIVISION.
           PROGRAM-ID. FORMATTER.
           LINKAGE SECTION.
           PROCEDURE DIVISION.
               MOVE WS-TOTAL TO WS-RESULT
               GOBACK.
           END PROGRAM FORMATTER.

       END PROGRAM BUGGY.

Bugs to find: 1. Parameter passing issue in the CALL to ADDER 2. Missing attribute on a nested program 3. Incorrect termination statement in a nested program 4. Scope/visibility issue in FORMATTER 5. Calling convention issue between SHOWER and FORMATTER

Document each bug, explain what would happen, and provide the corrected code.