← Back to course
Code Crew G5 / Week 02 / Crew Quest
6/6
Week 02 Β· Functions call functions

Crew Quest

Crew Quest: Tray tower
// Who waits, who returns
⏱ about 15 min

Crew Quest: Tray tower

On Saturday the galley is quiet. Rocket stacks paper plates on the table.

"Let us act out the call stack," he says. "Each person is a function."

Raven hands out sticky notes. "What do you notice when the stack gets taller?"

"More people are waiting," Rocket says. "Only the top person gets to talk."

Nova blinks happily. "Here is a hint. Nobody leaves until the person above them has returned."

  1. A function can call another function, called a helper.
  2. The caller waits on that line until the helper returns.
  3. The call stack is a pile of trays. New calls go on top. Returns come off the top.
  4. Inside calls run first, and each return goes back to its caller.
  5. A helper that prints instead of returning gives back None.
β—‡ TRAY TOWER
You need three people and three paper plates or sheets of paper.
Write launch, scanner and lens on the plates.
Launch says "launch start" and calls scanner. Scanner puts a plate on the pile.
Scanner says "scanner start" and calls lens. Lens adds the top plate and says "lens clean".
Each person takes their plate off and says "done" when they return.
For a grown-up
Only the person whose plate is on top may speak. This one rule is how the call stack works.
RECAP QUESTION
  • Read the question.
  • Tap your answer.
Function a calls b, and b calls c. Which function returns first?
Bonus recap statementTrue or false?
A caller waits until its helper returns.?
The first tray on the stack comes off first.?
Inside calls run first.?
A function with no return gives back None.?
WHY THIS EXERCISEThese four rules explain every program this week.
a calls b, and b calls c. How many trays are on the stack when c runs?
square(n) returns n * n. What is square(square(2))?
add(a, b) returns a + b. What is add(square(3), 1)?

Quest complete! Next week, a function calls itself, like a box with a smaller box inside.

← Friday