Lab 11: Fruitful Recursion
Worksheet Solutions
Python Solutions
Summary
- Part 0: Worksheet warm-up
- Part 1: Using the Debugger
- Part 2: Fruitful Recursion
No download folder for today.
Create a new file called lab11.py
and write all your code for today's lab in that file.
Table of Contents
Big Questions
-
In a fruitful recursive function, which function frames contribute to the value returned by the original function frame?
Show Answer
The answer here is "all of them." Although only the original function frame directly returns the result value, it will typically use information returned by other function frames to construct that value, so all of the function frames end up contributing to the eventual result somehow. - Why can't we use the early return pattern in a recursive function like we
can if we're using a loop to repeat code?
Show Answer
When we're writing code that uses a loop, as long as that code is inside of a function, a
return
statement will exit the entire function, thereby ending the loop. However,return
only exits a single function call frame, and since recursion makes use of multiple function call frames to do its work,return
will only exit one of them, effectively returning to an earlier "iteration" of the code (although of course it's a bit more complicated than that).