You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers recursion — a fundamental programming technique where a function calls itself. Recursion is a key topic in the OCR A-Level Computer Science (H446) specification and underpins many algorithms and data structures.
Recursion is a technique where a function calls itself to solve a smaller version of the same problem. Every recursive function has two essential components:
| Component | Description |
|---|---|
| Base case | A condition that stops the recursion (prevents infinite calls). |
| Recursive case | The function calls itself with a smaller/simpler input. |
Think of Russian nesting dolls (matryoshka). To find the smallest doll, you open the outer doll (recursive call), then the next, and the next, until you reach a doll that cannot be opened (base case).
function solve(problem)
if problem is simple enough then
return simple answer // BASE CASE
else
simplify the problem
return solve(simpler problem) // RECURSIVE CASE
endif
endfunction
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.