This section explains, in a way that meets the Cambridge AS/A‑Level assessment objective 2 (show awareness of what a compiler has to do), the steps a compiler follows when it encounters a recursive function. The material is deliberately scoped to the depth required for the syllabus, with an optional “Enrichment” box for students who wish to explore the topic further.
Each invocation of a recursive function receives its own activation record. The typical layout on a word‑addressable CPU is shown below.
| Field | Purpose | Typical size (bytes) |
|---|---|---|
| Return address | Instruction to continue after the call returns | Word size (4 or 8) |
| Saved frame pointer (FP) | Link to the caller’s activation record | Word size |
| Parameters | Values passed to the recursive call (or space for them) | Variable |
| Local variables | Variables declared inside the function | Variable |
| Temporaries / saved registers | Intermediate results and caller‑saved registers | Variable |
If the recursive call is the last operation performed by a function, the compiler can replace the normal call/return sequence with a simple jump. This re‑uses the current activation record, turning an otherwise unbounded recursion into an iterative loop at the machine level.
A function f is tail‑recursive when it can be written as:
$$ f(x)= \begin{cases} g(x) & \text{if base case}\\[4pt] f\bigl(h(x)\bigr) & \text{otherwise (tail call)} \end{cases} $$After TCO the stack depth is constant: O(1) instead of O(n) for n recursive calls.
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
if statement, the multiplication, and the self‑call.n is an int, the return type matches, and the function is visible to its own body.nfactorial(n‑1)
; ----- prologue -----
push FP ; save caller's frame pointer
mov FP, SP ; establish new frame pointer
sub SP, #locals ; allocate space for locals
; ----- base case -----
cmp n, #0
jne recurse
mov R0, #1 ; return 1
jmp end
; ----- recursive case -----
recurse:
mov R1, n ; preserve original n
dec n, #1 ; n‑1 as argument
push n ; pass argument
call factorial ; recursive call
add SP, #4 ; clean argument
mul R0, R1 ; R0 = n_original * factorial(n‑1)
; ----- epilogue -----
end:
mov SP, FP ; de‑allocate locals
pop FP ; restore caller's FP
ret
call instruction automatically stores the return address.| Compilation phase | What the compiler does | Effect on recursion |
|---|---|---|
| Lexical analysis & parsing | Creates tokens and an AST; recognises self‑references. | Allows the compiler to identify a recursive call. |
| Semantic analysis | Type‑checks arguments/return values; checks visibility. | Prevents type‑related run‑time errors in recursive calls. |
| Activation‑record design | Allocates stack‑frame layout for parameters, locals, temporaries, saved registers. | Provides a separate context for each recursion level. |
| Code generation | Emits instructions for argument evaluation, call/return handling, and stack manipulation. | Implements the actual recursion mechanism at machine level. |
| Optimisation | Applies tail‑call optimisation, inlining, or recursion‑to‑iteration transformation. | Can reduce stack usage (O(1) instead of O(n)) or replace recursion with a loop. |
| Target‑code emission | Outputs machine code, assembly, or IR ready for the linker/loader. | Delivers an executable that correctly handles any depth of recursion (subject to hardware limits). |
To place the recursion topic in context, the table below lists the AS‑Level (units 1‑12) and A‑Level (units 13‑20) components of the Cambridge International Computer Science specification. The notes that follow give a brief “key concepts” snapshot for each unit, together with the assessment objectives (AO1–AO3) that the material supports.
| Unit | Title (AS) | Key concepts | Relevant AO(s) |
|---|---|---|---|
| 1 | Information Representation | Binary, hexadecimal, two’s‑complement, floating‑point, character encodings, data compression. | AO1, AO2 |
| 2 | Communication and Networking | OSI model, protocols (TCP/IP, HTTP), bandwidth, latency, security basics. | AO1, AO2 |
| 3 | Hardware Fundamentals | CPU architecture, registers, ALU, cache, storage hierarchy, I/O devices. | AO1, AO2 |
| 4 | Processor Fundamentals | Instruction set, pipelining, addressing modes, basic assembly language. | AO1, AO2 |
| 5 | System Software | Operating system functions, file systems, compilers, interpreters, virtual machines. | AO1, AO2 |
| 6 | Security | Encryption, authentication, threats, counter‑measures, ethical considerations. | AO1, AO2, AO3 |
| 7 | Ethics and Legal Issues | Intellectual property, privacy, professional codes of practice. | AO1, AO3 |
| 8 | Databases | Relational model, SQL, normalisation, indexing, data integrity. | AO1, AO2, AO3 |
| 9 | Algorithm Design | Flowcharts, pseudocode, control structures, complexity (Big‑O), searching & sorting. | AO1, AO2, AO3 |
| 10 | Data Types & Structures | Arrays, records, linked lists, trees, stacks, queues, abstract data types. | AO1, AO2, AO3 |
| 11 | Programming | Procedural programming, recursion, iteration, debugging, testing. | AO1, AO2, AO3 |
| 12 | Software Development | System life‑cycle, methodologies (waterfall, agile), documentation, version control. | AO1, AO2, AO3 |
The A‑Level units (13‑20) extend these foundations with more depth (e.g., advanced data representation, virtual machines, artificial intelligence, computational thinking). The same structure—key concepts plus AO mapping—applies, and teachers can expand each unit into a dedicated set of notes.
For students who wish to go beyond the syllabus requirement, the following topics provide a glimpse of real‑world compiler engineering:
These topics are **not** required for the Cambridge exam but can enrich a student’s understanding and are useful for further study in computer science.
Create an account or Login to take a Quiz
Log in to suggest improvements to this note.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.