Rocket writes a function to clean teeth. Inside it, he calls brush and rinse, then brush and rinse again. "Functions inside functions!" he cheers.
Raven reads it slowly. "What do you notice about brush?" she asks. "It shows up more than once," Rocket says.
Nova lights up each call. "Count one line at a time. Then look for a chunk that repeats." Let's help them count the calls.
Functions can call OTHER functions. Read this carefully:
function brush(): move toothbrush 20 times
function rinse(): sip water, swish, spit
function clean_teeth():
brush()
rinse()
brush()
rinse() Look at this messy code. There's a repeated chunk that should be a function.
Underline the repeat. Give it a NAME.
open the door say hello wave hand ... open the door say hello wave hand ... open the door say hello wave hand
A function can call another function. To count, start with the smallest one.
function clap2():
clap
clap
function cheer():
clap2()
jump
clap2() | Statement | True or false? |
|---|---|
| A function can call another function. | ? |
| Changing clap2 changes every cheer(). | ? |
| A repeated chunk of steps is a clue for a function. | ? |
| cheer() can run without running clap2(). | ? |