← Back to course
Code Crew G5 / Week 03 / Crew Quest
6/6
Week 03 Β· Nesting boxes

Crew Quest

Crew Quest: Nesting notes
// A function that calls itself
⏱ about 15 min

Crew Quest: Nesting notes

Raven has a surprise for the crew. She hands Rocket a folded paper packet.

He unfolds it. Inside is another folded note, and another inside that.

"Each note says: open the next note," Rocket reads. "Except this one!"

Raven laughs. "What do you notice about the last note?"

Nova circles the tiny paper. "Here is a hint. The last note is the one that says stop."

  1. Recursion solves a problem by solving a smaller copy of the same problem.
  2. A recursive function calls itself with a smaller value.
  3. One case does not call again. It is the stopping point.
  4. Calls stack up going down, then finish in reverse order coming back up.
  5. Lines before the call print going down. Lines after it print coming back up.
β—‡ NESTING NOTES
Take four sheets of paper of different sizes, or fold sheets smaller and smaller.
On the three biggest, write: open the next note.
On the smallest, write a secret word and: stop here.
Fold each note inside the next bigger one.
Give the packet to your partner. Count out loud as each note opens.
For a grown-up
No scissors are needed. Folding works fine. Ask your child which note is the stopping point, and why the packet needs one.
RECAP QUESTION
  • Read the question.
  • Tap your answer.
countdown(2) calls countdown(1). What does countdown(1) call?
Bonus recap statementTrue or false?
A recursive function calls itself.?
Every call works on the same size of problem.?
The stopping point does not call again.?
The deepest call finishes first.?
WHY THIS EXERCISESmaller problems and a stopping point make recursion work.
How many lines does countdown(4) print?
What is the first line count_up(2) prints?
How many stars does stairs(3) print in all?

Quest complete! Next week we learn the two parts every recursion needs.

← Friday