Easytrieve Nested Procedures

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.

Progress0 of 0 lessons

How Nesting Works

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.

text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
JOB 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.

The No-Recursion Rule

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.

Allowed versus forbidden call shapes
Call shapeAllowed?Notes
JOB → PROCA → PROCBYesClassic one-level nest
JOB → A → B → CYesMulti-level if non-cyclic
A → B → ANoDirect recursion forbidden
A → B → C → ANoIndirect recursion forbidden
PROCA → PROCA via PERFORMNoSelf-call is recursion

Design Patterns for Nested Modules

Layered validation

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.

Calculation pipeline

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.

Shared utility PROCs

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.

When to Flatten Instead of Nest

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.

  • Nest when two or more callers need identical logic.
  • Flatten when a PROC exists only to wrap one IF already unique to its caller.
  • Nest utilities at the bottom of the activity with clear one-way dependencies.
  • Avoid nest chains where each level passes only one flag—merge into one readable PROC.

Nesting From Report and Screen PROCs

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.

text
1
2
3
4
5
6
7
8
9
10
REPORT 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

Tracing Nested Calls for Debugging

  1. Draw a PERFORM graph on paper: nodes are PROC labels, edges are PERFORM direction.
  2. Verify no path returns to an ancestor node—recursion check before compile.
  3. Add temporary audit fields set at each PROC entry with unique tokens for test runs.
  4. Compare compile listing procedure tables if your site generates cross-references.
  5. Step through one input record in test explaining each END-PROC return aloud to a peer.

Common Nested Procedure Mistakes

  • Indirect recursion through three modules without noticing the cycle.
  • PERFORM across activities expecting linkage that scope rules deny.
  • Deep nesting that duplicates field names with different meanings at each level.
  • Utility PROC that PERFORMs orchestrator PROC for convenience—creates cycle risk.
  • Replacing loops with repeated PERFORM of the same PROC expecting iteration—use JOB loop instead.
  • Forgetting inner END-PROC so outer module never returns correctly.

Explain It Like I'm Five

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.

Exercises

  1. Draw the PERFORM graph for A→B→C and mark where each END-PROC returns.
  2. Identify recursion in a flawed A→B→A sample and rewrite using a JOB loop counter.
  3. Refactor a four-level nest into two PROCs plus one utility without losing behavior.
  4. Code BEFORE-LINE PERFORMing a shared rounding utility PROC.
  5. Write a team standard for maximum recommended nesting depth with rationale.

Quiz

Test Your Knowledge

1. Procedure A may PERFORM procedure B when:

  • Both are defined in the same activity
  • B is in a different JOB activity
  • Only in Library
  • Never

2. Recursion in Easytrieve PROCs is:

  • Not permitted
  • Unlimited
  • Required for loops
  • Only in SCREEN

3. When PROCB END-PROC runs inside a call from PROCA, control returns to:

  • Statement after PERFORM PROCB inside PROCA
  • Top of JOB
  • END-PROC of PROCA immediately
  • JCL

4. Deep nesting beyond three levels should prompt:

  • Review whether modules should flatten or merge
  • Automatic compile failure
  • Use of JCL PROC
  • Removal of END-PROC

5. PERFORM inside BEFORE-LINE. PROC to a user PROC is valid when:

  • The user PROC is in the same activity
  • The user PROC is in Library only
  • Never
  • Only on CICS
Published
Read time15 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve Report Generator 11.6 nested PROC and recursion rulesSources: Broadcom Easytrieve 11.6 Language Reference PROC, PERFORM, procedure nestingApplies to: Easytrieve nested procedure modules