Functions: Your Own Commands
A function is a named recipe you can use again and again. Learn about inputs, outputs and return values, and write a function of your own.
Welcome back!
Step 1 of 10Learn
Make your own command
Imagine you teach your robot the steps for a dance: spin, clap, jump. Instead of listing all three every time, you give them a name: dance. Now you just say "dance" and the robot does all three.
That is a function: a group of instructions with a name. You write it once and use it as many times as you like. Using a function is called calling it.
Step 2 of 10Practice · Choose one
Why do coders use functions?
Step 3 of 10Learn
Inputs and outputs
Many functions take an input and give back an output. Think of a juice machine: you put in an orange (input), and out comes juice (output).
function double(n) {
return n * 2;
}The input is called n. The * sign means multiply. Calling double(4) puts 4 into n, and the function returns 8. Calling double(10) returns 20. Same function, different inputs, different outputs.
Step 4 of 10Practice · Match the pairs
double(n) returns n * 2. Match each call to its output.
Tap an answer, then tap the place where it belongs (or drag it there).
- double(3)
- double(5)
- double(0)
- double(50)
Step 5 of 10Practice · Code challenge
Write the body of triple(n) so it returns n times 3. Use * to multiply.
triple(2)triple(10)triple(0)
Step 6 of 10Note
Step 7 of 10Flashcards
Function words
Tap a card to flip it.
Step 8 of 10Practice · Fill in the blanks
Fill in the blanks
The value a function gives back is called its . The value you put in is its .
Step 9 of 10Practice · Code challenge
Make sayHi(name) return the text Hi, followed by the name. For example sayHi("Sam") should return "Hi, Sam". Use + to join text.
sayHi("Sam")sayHi("Ava")
Step 10 of 10Practice · What would you do?
What would you do?
Your game shows a "You win!" message in 6 different places, each written out with the same 5 lines of code. Now you want to change the message color. What is the smartest plan?
Lesson complete!