Follow a recursive call through its stack frames
This activation-record visualizer shows a small factorial program as calls create frames and returns remove them. A loaded example appears immediately. Change the factorial input, then use First, Previous, Next, Last or the execution slider to inspect the stack at a particular event. The result and maximum active factorial-frame count are computed from the same trace as the diagram.
A second panel compares call by value, reference, value-result and name. It runs two fixed examples: an array swap whose index changes during the call, and a procedure with aliased actual arguments. These examples make the differences visible without requiring you to learn another programming-language syntax. This is a deterministic teaching simulation, not an interpreter for pasted programs or a debugger attached to your computer.
Read the fields of an activation record
Each factorial call receives a separate frame with a symbolic identity. The parameter n belongs to that invocation. The local child field records the recursive call's result once the caller resumes. Return value stays uncomputed until the base case or multiplication has produced it. Waiting callers retain their own parameters while the deepest call is running.
The return address says where execution resumes after the call. The control link identifies the caller: main for the initial invocation, then the preceding factorial frame for recursive invocations. These are symbolic names rather than instruction addresses, machine registers or memory offsets. The current frame appears at the top of the stack display, even though snapshots internally retain calls in entry order.
Distinguish control links from access links
A control link follows the dynamic call chain. An access link represents the lexical environment used for nonlocal bindings. The displayed factorial procedure is declared at global scope, so every symbolic access link points to global. Recursion does not make an earlier factorial activation the lexical parent of the new activation.
A real runtime may omit a physical access-link field when global addressing is sufficient. This diagram includes it to explain the distinction; it does not prescribe a calling convention or frame layout for C, Java or a particular processor. Nested procedures would require another lexical-scope example, which this page does not simulate. Do not infer static nesting from whichever frame happened to call a function.
Worked factorial example
For the loaded input, factorial(4) returns 24 and reaches 4 active factorial frames. The deepest stack has parameters 4, 3, 2, 1 in call order. The base case returns a unit result; each suspended caller receives its child result before multiplying by its own saved parameter.
| Returning parameter | Computed return value |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 6 |
| 4 | 24 |
The final step shows an empty factorial stack because the initial call has returned to main. That does not mean the result disappeared. The status line still shows it. Inputs are whole numbers from zero through twelve; zero follows the same base-case rule as one. Exact integer multiplication is used inside this bounded model.
Compare four parameter-passing rules
Call by value copies the incoming values into independent formal locations. Assigning a formal changes that local copy. Call by reference binds a formal to an actual location at entry, so a write is visible to the caller immediately. In the array example, the referenced element is chosen before the procedure body changes the index.
Value-result also uses local copies during the body, then copies the final formal values back. This simulator captures each actual destination at call entry and exposes the copy-out order as a control. Call by name instead uses read/write accessors that re-evaluate an actual location on each access. For the changing-index array argument, that can select a different element later in the same call.
Worked swap with a changing array index
The example calls swap(i, a[i]) with i equal to the initial index shown in the panel. The body saves first into temp, assigns second into first, then assigns temp into second. Starting values and every intermediate state come from the parameter model, including the formal locations shown in the detailed trace.
| Passing rule | Final i | Final a[1] | Final a[2] |
|---|---|---|---|
| value | 1 | 2 | 3 |
| reference | 2 | 1 | 3 |
| value-result | 2 | 1 | 3 |
| name | 2 | 2 | 1 |
With name passing, assigning first changes i before second is assigned. The later access to a[i] therefore uses the new index. It does not perform the same exchange as a reference captured at entry. Select a passing mode to inspect the formal values and locations beside the caller state after every statement.
Aliasing makes copy-out order observable
The second example calls p(x, x), starting with x equal to zero. The body assigns one to first and then two to second. Reference and name share the caller's scalar location throughout the call. Value uses independent locals and performs no copy-back. With left-to-right copy-out, the computed final values are:
| Passing rule | Final x |
|---|---|
| value | 0 |
| reference | 2 |
| value-result | 2 |
| name | 2 |
Switch value-result to right-to-left copy-out and its final x becomes 1. The copies are independent during the body, but the two destinations alias. The last copy wins. These are explicitly chosen model rules; a real language may define another convention or reject such calls. Do not treat an unspecified copy-out order as a universal language guarantee.
Check your answer and continue the compiler lab
Track locations as well as values. Keep a suspended caller's local data separate from the current callee. Do not erase a return value before the caller receives it, or confuse lexical links with recursive callers. In parameter exercises, state when an array destination is chosen and whether writes happen during the call or at return.
Use the three-address code generator to explore intermediate instructions, the basic-block flow graph to inspect control flow, and the simple code generator for register and address descriptors. The compiler lab groups the related stages. Copy comparison saves the factorial context, the selected example, the copy-out convention and all four final caller states.