Nesting means a PROC module PERFORMs another PROC before its own END-PROC runs. Payroll programs nest constantly: VALIDATE-EMP PERFORMs LOOKUP-STATE-TAX, which PERFORMs READ-TAX-TABLE. Broadcom allows this stacking but forbids recursion—no module may call itself directly or through a chain that loops back. This tutorial teaches call chain mechanics, return order with multiple END-PROC levels, when nesting clarifies design versus when flattening helps maintenance, nesting from report hooks, and anti-patterns that mimic recursion with counters. Understanding nested procedures separates modular Easytrieve from spaghetti PERFORM graphs support teams refuse to touch.
PERFORM saves an implicit return address, jumps to the target PROC, executes until END-PROC, then resumes. Nesting repeats that inside the callee. Only one PERFORM path is active per thread of execution, but nested calls stack return points like trays on a cart. Each END-PROC pops one level. The outermost END-PROC eventually returns to JOB logic after the original PERFORM that started the chain.
123456789101112131415161718192021JOB INPUT SALES PERFORM PROCESS-RECORD PROCESS-RECORD. PROC PERFORM VALIDATE-KEYS IF ERROR-FLAG EQ 'N' PERFORM CALC-COMMISSION END-IF END-PROC VALIDATE-KEYS. PROC IF CUST-ID EQ SPACES ERROR-FLAG = 'Y' ELSE ERROR-FLAG = 'N' END-IF END-PROC CALC-COMMISSION. PROC COMM-AMT = SALES-AMT * COMM-RATE END-PROC
For a valid record, order is PROCESS-RECORD, VALIDATE-KEYS, END-PROC, CALC-COMMISSION, END-PROC, END-PROC back to implied JOB loop. ERROR-FLAG set in VALIDATE-KEYS remains visible in PROCESS-RECORD because modules share storage.
Broadcom states explicitly that procedure A may call B, but B cannot call A—neither directly nor through intermediate modules that eventually reach A. Attempted recursion produces unpredictable results because the environment does not implement a safe recursive call stack for PROC modules. If you need repeated execution, use the implied JOB INPUT loop, DO WHILE, or GOTO JOB patterns documented in control flow chapters—not PERFORM PROCA from PROCB when PROCB is reachable from PROCA.
| Call shape | Allowed? | Notes |
|---|---|---|
| JOB → PROCA → PROCB | Yes | Classic one-level nest |
| JOB → A → B → C | Yes | Multi-level if non-cyclic |
| A → B → A | No | Direct recursion forbidden |
| A → B → C → A | No | Indirect recursion forbidden |
| PROCA → PROCA via PERFORM | No | Self-call is recursion |
Outer PROC orchestrates; inner PROCs each check one concern—key presence, date range, status code. Outer module branches on ERROR-FLAG after each inner return. Keeps each END-PROC block small and testable.
Chain arithmetic steps: GROSS → ADJUSTMENTS → TAX → NET. Each step PROC reads prior fields and writes the next. Nesting order documents pipeline sequence in PERFORM order inside a master CALC-PAY. PROC.
FORMAT-DATE and ROUND-MONEY utility modules sit at the end of the activity; many higher PROCs PERFORM them. Utilities should not PERFORM orchestrators above them—keep dependency direction one-way to avoid accidental cycles.
Flattening inlines PERFORM targets or merges small one-statement PROCs into callers. Prefer flattening when nesting depth exceeds three without domain reason, when only one caller exists for a tiny module, or when call graph drawings become cyclic-looking even if technically acyclic. Prefer nesting when inner logic repeats from multiple callers, when report hooks and JOB logic share a utility, or when inner module names document business rules auditors recognize.
BEFORE-LINE. PROC may PERFORM CALC-RUNNING-TOTAL defined as a user PROC in the same JOB activity. That nest keeps arithmetic out of report hook syntax restrictions. Screen AFTER-SCREEN validation may PERFORM shared error-message formatters. Scope still applies: report hook cannot PERFORM a PROC from a different JOB activity even if that activity appears later in the same source member.
12345678910REPORT DETAIL-RPT LINE DEPT EMP-NAME SALES BEFORE-LINE. PROC PERFORM ACCUM-DEPT-SALES END-PROC ACCUM-DEPT-SALES. PROC DEPT-TOTAL = DEPT-TOTAL + SALES END-PROC
Nesting is asking a friend for help while you are still doing your chore. You pause your list, your friend does their smaller list, says done, and you continue yours. You can ask another friend from your list if needed. But you cannot ask yourself to start your same list over from inside your list—that would never finish. That is why recursion is not allowed.
1. Procedure A may PERFORM procedure B when:
2. Recursion in Easytrieve PROCs is:
3. When PROCB END-PROC runs inside a call from PROCA, control returns to:
4. Deep nesting beyond three levels should prompt:
5. PERFORM inside BEFORE-LINE. PROC to a user PROC is valid when: