Why must a recursive step always be paired with a base case — and what holds four unfinished calls in the air until it lands?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
A recursive function stops when its second argument b is exactly 1 — that is its base case. Its recursive step calls itself with the same first argument and a new second argument. Starting from b = 8, decide which of these replacements for that second argument eventually land the chain of calls exactly on 1:
b - 1bb - 2b // 2b + 1Write out the first few values of b for each candidate before you decide — // is integer division, which throws away any fraction. Assign to reaches_base a list of the letters that do, as uppercase strings, in alphabetical order.
The conquer step back up from 5*1
Record the ascent with addition only: 5*1 is 5, then 5 plus 5 is 10, then 5 plus 10 is 15, then 5 plus 15 is 20.
Recursive step a*b = a + a*(b-1) with a base case
Write the two-line definition, a*b = a + a*(b-1) when b is not 1 and a*b = a when b == 1, and state that the recursive step alone runs forever.
A function calling itself with a changed argument
Write mult_recur(a, b) returning a when b == 1 and a + mult_recur(a, b-1) otherwise, and contrast that call with mult_recur(a, b), which makes no progress.
The four stacked frames of mult_recur(5,4)
Trace the calls with b equal to 4, 3, 2 and 1, writing the pending 5 + expression each frame holds and the order the frames finish after the base case returns.