Six original Cambridge-style questions on recursion: features, traces, pseudocode and the call stack.
State the three essential features of a recursive subroutine.
Trace the function below for DigitSum(4721), showing each call, the value it waits for and the value it returns.
FUNCTION DigitSum(n : INTEGER) RETURNS INTEGER
IF n < 10 THEN
RETURN n
ELSE
RETURN (n MOD 10) + DigitSum(n DIV 10)
ENDIF
ENDFUNCTIONData : ARRAY[1:100] OF INTEGER is a global array in ascending order. Write pseudocode for a recursive function RBinarySearch(Target : INTEGER, Low : INTEGER, High : INTEGER) RETURNS INTEGER that returns the index of Target, or −1 if it is not present. It is first called as RBinarySearch(Target, 1, 100).
IF Low > High THEN RETURN -1. ✓Mid ← (Low + High) DIV 2. ✓Data[Mid] = Target, RETURN Mid (second base case). ✓Data[Mid] < Target, RETURN RBinarySearch(Target, Mid + 1, High). ✓RETURN RBinarySearch(Target, Low, Mid - 1). ✓The array in the binary search lesson holds 3, 8, 12, 17, 21, 26, 30, 34, 39, 42, 47, 51, 58, 63, 70 in elements 1 to 15. For your function in the previous question (adapted to this array), state the arguments of every call made by RBinarySearch(9, 1, 15) and the value finally returned.
Explain how a compiler handles a recursive call, and why recursion can cause a run-time error.
A student writes a recursive function to add up the 1 000 000 values in an array, one element per call. Evaluate this design.
Mark this once you have attempted all six and checked your working. It records a Practiced badge on the topic and adds a one-time bonus. Revealing the solutions alone does not count.