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 powerful programming technique in which a function calls itself to solve a problem. Recursion is a key topic in A-Level Computer Science and is closely linked to mathematical induction, tree traversal, and divide-and-conquer algorithms.
Recursion is a technique where a subroutine (function or procedure) calls itself as part of its execution. Every recursive solution must have:
Without a base case, recursion would continue indefinitely, causing a stack overflow error.
The factorial of a non-negative integer n (written n!) is defined as:
FUNCTION factorial(n: INTEGER) RETURNS INTEGER
IF n = 0 THEN
RETURN 1 // Base case
ELSE
RETURN n * factorial(n - 1) // Recursive case
END IF
END FUNCTION
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.