Defining recursion, base and general cases, the call stack, tracing recursive calls, comparing recursion with iteration, and worked recursive algorithms.
What is recursion?
A recursive subroutine is one that calls itself. Every correct recursion needs:
a base case — a condition that stops the recursion (no further call), and
a general (recursive) case — which calls the routine again with a smaller/simpler argument that moves towards the base case.
Without a reachable base case the recursion never stops and causes a stack overflow.
`` FUNCTION Factorial(n) RETURNS INTEGER IF n = 0 THEN // base case RETURN 1 ELSE RETURN n * Factorial(n - 1) // general case ENDIF ENDFUNCTION ``
Viewing only
This content is free to read on superexams.com and cannot be printed or downloaded.
Read the full note, free
Create a free account to read this note in full. Every free account gets 2 complete revision notes, no card needed.