Skip to the main content
PeabodyMake a free story

🤖 Code & Robots, level 7

What the trails say and what they ask. Print it for the class, or put the same questions on the board with a live quiz.

Code & Robots, level 7

🧩 Functions

How do coders avoid writing the same steps again and again?

  • How do coders avoid writing the same steps again and again? They wrap those steps in a function. A function is a set of steps with a name, so a program can run all of the steps just by using that name. Think of a recipe card for pancakes, because once the card is written, you can simply say "make pancakes" instead of reading out every single step each time. When a program uses a function, coders say that the program calls it. You can call the same function many times, from many places in a program. Writing the steps once and reusing them saves time, and it also makes mistakes easier to fix, because a fix in one place fixes every call.
  • Many functions need some information to do their job. The information you hand to a function is called an input, and the name the function uses for that input is called a parameter. Here is a short function in a made-up code: define double(n): return n * 2. Its name is double, and n is its parameter. The word return means the function hands back an answer, which is called its output, or its return value. So when a program calls double(5), the function sets n to 5, works out 5 times 2, and hands back 10. If the program calls double(7) instead, it hands back 14, because the steps stay the same while each new input gives a new output.
  • A function can also take more than one input. Picture a function called area(width, height) that returns width * height, which is the area of a rectangle. When you call area(3, 4), width becomes 3 and height becomes 4, so the function returns 12. The order of the inputs matters, since the first input fills the first parameter and the second fills the second. To trace a function call, you write down each parameter's value, follow the steps, and note what comes back. A return value can even be used inside another call. For example, double(area(2, 3)) first works out area(2, 3), which is 6, and then doubles it to get 12. Big programs are built from many small functions like these, and each one does a single job.

Try it at home: Make up a family function together, like tripleThenAddOne(n): call it with a few numbers out loud and trace what comes back.

Questions and answers

Functions

  1. A set of steps with a name, which a program can run by using that name, is called a… function (A function wraps a set of steps under one name.)
  2. 📞 When a program uses a function, coders say the program… calls it (Using a function is called calling it.)
  3. In define double(n): return n * 2, what is n? A parameter (n is the name the function uses for its input, which is a parameter.)
  4. ✖️ double(n) returns n * 2. What does double(6) return? 12 (n becomes 6, and 6 times 2 is 12.)
  5. 📤 The answer a function hands back is called its… return value (The output a function hands back is its return value.)
  6. 📐 area(width, height) returns width * height. What does area(5, 2) return? 10 (width becomes 5 and height becomes 2, and 5 times 2 is 10.)