A supply crate arrives from home base. A note on top says: the new star map chip is inside.
Rocket lifts the lid. Inside is another box. He opens that one. Inside is another box!
"How many boxes are there?" he groans. "I need a different plan for every box!"
Raven picks up the next box. "What do you notice about each box you open?"
Nova hovers over the pile. "Here is a hint. Maybe one plan works for every box."
Here is the crate, drawn from the outside in. Each box holds one smaller box, until the last one.
Rocket does not need a new plan for each box. One plan works for all of them.
Look at step 3. The plan uses itself on a smaller box. This idea is called recursion.
Step 2 is just as important. It tells you when to stop.
| Statement about the box plan | True or false? |
|---|---|
| The same three steps work for a crate with 100 boxes. | ? |
| Each box inside is bigger than the one around it. | ? |
| Without step 2, the plan would never say stop. | ? |
A recursive plan has two parts. One part does a small step and uses the same plan on something smaller. The other part says when to stop.
| Statement | True or false? |
|---|---|
| A recursive plan uses itself on a smaller problem. | ? |
| A recursive plan needs new steps for every box. | ? |
| The stopping step does not use the plan again. | ? |
| A crate of 3 boxes needs more runs than a crate of 6 boxes. | ? |
Wonderful! You just met recursion without a single line of code. Tomorrow we pass a question down a line of crew.