Home / Reasoning / Functions: Name a Group of Steps and Use It Again
Reasoning & Coding · Coding
Functions: Name a Group of Steps and Use It Again
Define a function, call it by name and give it an input. Count the commands it saves, trace robot paths and fix a bug once so it is fixed everywhere.
The idea
Think about a recipe card that says "make the dough". The card does not list every step of making dough again; you already know them. Programs can do the same thing. A function (some coding tools call it a procedure or a custom block) gives a name to a group of steps, so you can use the whole group by writing just the name.
In this lesson we use the robot from the robot-path lesson. F means move forward one square, L means turn left and R means turn right. A function is written like this: Define stair [F L F R]. The word after Define is the function's name, and the commands in square brackets are its body.
Defining a function only teaches the computer the steps. Nothing moves yet. To run the steps, you call the function by writing its name: stair. Every time the program says stair, the robot does F L F R. (In Scratch, the matching idea is a My Blocks custom block.)
A function can also take an input, a number you hand it when you call it. Define line(n) [Repeat n [F]] has an input called n. The call line(3) runs Repeat 3 [F], and line(5) runs Repeat 5 [F]. One function, many lengths.
Functions make programs shorter and easier to read, and they make bugs easier to fix. If the steps inside a function are wrong, every call is wrong. But when you fix the function in one place, every call is fixed too.
As in the earlier lessons, S is the start, G is the goal and dark squares are walls. Unless a question says otherwise, the robot starts on S facing right (east).
Worked examples
Example 1 · Name a repeating chunk
The robot must climb the staircase from S to G. Write the program using a function.
- Trace one step of the staircase: F moves right, L turns to face up, F moves up, R turns back to face right. That chunk is F L F R.
- The staircase has three steps, so the chunk happens three times.
- Name the chunk: Define stair [F L F R].
- Call it three times: stair stair stair (or Repeat 3 [stair]).
Define stair [F L F R], then stair stair stair. The robot reaches G.
Example 2 · Count what the function saves
Without a function, the staircase program is F L F R F L F R F L F R. How many commands is that, and how many does the main program need with the stair function?
- Count the written-out program: 4 commands in each step × 3 steps = 12 commands.
- With the function, the main program is just stair stair stair: 3 calls.
- The definition is written once (4 commands). The bigger saving comes when a function is called many times, and the program is easier to read.
12 commands written out; 3 calls with the function (plus a 4-command definition written once).
Example 3 · Give the function an input
Define line(n) [Repeat n [F]]. Program: line(3) R line(2). How many squares does the robot move, and which way is it facing at the end?
- line(3) runs Repeat 3 [F]: 3 squares to the right.
- R turns the robot to face down (south). A turn does not move it.
- line(2) runs Repeat 2 [F]: 2 squares down.
- Total moves: 3 + 2 = 5.
5 squares, and the robot ends facing down (south).
Example 4 · Fix the bug in one place
Define turnAround [L]. Program: F F turnAround F F. The robot was meant to come back to the start, but it does not. Find and fix the bug.
- Trace: F F moves 2 squares right.
- turnAround runs L, which is only a quarter turn. The robot now faces up, not back toward S.
- F F moves 2 squares up, away from the start.
- Turning around is a half turn: two quarter turns. Change the body to L L (R R also works).
- Every call to turnAround in any program is now fixed.
Change the function to Define turnAround [L L]. Then F F turnAround F F returns to S.
Common mistakes
Why this fails: Define only names the steps. The steps run when the function is called by name.
Why this fails: Each call runs the whole body again. stair stair stair runs F L F R three times.
Why this fails: line(3) and line(5) run the same body with different values of n. Put the input value wherever n appears in the body.
Why this fails: If the steps inside the function are wrong, patching one call leaves the other calls broken. Fix the definition once.
Why this fails: A name like thing or doStuff tells a reader nothing. stair, turnAround and line(n) say what will happen.
Foundation
Foundation practice
Define names the steps. Calling the name runs them.
1. What does a function do in a program?
- It gives a name to a group of steps so you can run them again by writing the name.
- It makes the robot move faster.
- It runs the steps only if a condition is true.
- It counts how many times a loop runs.
2. Define hop [F F L]. Program: hop hop. Which shows every command the robot runs?
- F F L
- F F F F L L
- F F L F F L
- hop F F L
3. Define jump [F F]. Program: jump R jump. How many squares does the robot move forward?
- 2
- 3
- 5
- 4
4. Program: Define spin [R R R R]; F F. How many times does the robot turn?
- 4
- 0
- 8
- 1
5. Define stair [F L F R]. Which program takes the robot from S to G?
- stair stair
- stair stair stair stair
- Repeat 3 [F L F]
- stair stair stair
Practice
Practice practice
Trace each call, including the input value.
6. Define corner [F F R]. Program: corner corner corner corner. If you wrote the program out without the function, how many commands would it have?
- 4
- 7
- 12
- 16
7. Define line(n) [Repeat n [F]]. Program: line(3) R line(2). How many squares does the robot move?
- 3
- 5
- 6
- 2
8. Define line(n) [Repeat n [F]]. Which call moves the robot from S to G?
- line(4)
- line(6)
- line(5)
- line(2) line(2)
9. Define double(x) [say x + x]. Program: double(3) double(5). What does the program say?
- 3, then 5
- 16
- 9, then 25
- 6, then 10
10. Define line(n) [Repeat n [F]]. Which program takes the robot from S to G?
- line(2) R line(2)
- line(3) R line(2)
- line(2) L line(2)
- R line(2) L line(2)
11. Define ??? [F R F R F R F R]. The robot ends where it started. Which name describes the steps best?
- moveRight
- square
- fourSteps
- stair
Think
Think practice
Find the bug inside the function and explain your fix.
12. Define turnAround [L]. Program: F F turnAround F F. It should bring the robot back to the start. Which change to the function fixes it?
- Change it to L L
- Change it to R
- Change it to L R
- Change it to F L
13. A program calls turnAround in 6 different places, and the function has a bug. How many places must you change to fix every call?
- 6
- 1
- 7
- 0
14. Define line(n) [Repeat 3 [F]]. Program: line(5). The robot moves only 3 squares. What is the bug?
- The program should call line(3).
- Repeat cannot be used inside a function.
- line needs to be called five times.
- The body uses 3 instead of n, so the input is ignored.
15. Why is it better to write a function than to copy and paste the same steps in many places? Give two reasons.
Challenge
Challenge practice
Functions can call other functions. Trace carefully.
16. Define corner [F F R]. Define square [Repeat 4 [corner]]. Program: square square. How many forward moves happen, and where does the robot finish?
- 16 moves, back at the start facing right
- 8 moves, back at the start facing right
- 16 moves, at the opposite corner of the square
- 32 moves, back at the start facing left
17. Define zigzag [F R F L]. The robot faces right. Program: zigzag zigzag zigzag. Where does it end, compared with where it started?
- 6 squares right
- 3 squares right and 3 squares up
- 3 squares right and 3 squares down
- 6 squares right and 6 squares down
18. Write a function for one step of a staircase that climbs up and to the right, then use it to climb 4 steps. The robot starts facing right.
Answer key
- 1. It gives a name to a group of steps so you can run them again by writing the name.
A function names a chunk of steps for reuse. Choosing steps only when a condition is true is an if–else, and counting passes is a loop's job. Speed does not change. - 2. F F L F F L
Each call runs the whole body in order, so two calls give F F L, then F F L again. F F L is only one call, and F F F F L L changes the order of the steps. - 3. 4
Each jump is 2 forward moves, and there are 2 calls: 4. R turns the robot but does not move it, so 5 counts the turn as a move, and 2 counts only one call. - 4. 0
spin is defined but never called, so its turns never run. The program only runs F F. 4 assumes defining a function also runs it. - 5. stair stair stair
The staircase has three steps, so stair is called three times. Two calls stop one step short, four calls run off the grid, and Repeat 3 [F L F] keeps turning left and never turns back to face right, so the robot runs off the left edge of the grid. - 6. 12
Each call is 3 commands, and there are 4 calls: 3 × 4 = 12. 4 counts the calls, 7 adds 3 + 4, and 16 assumes 4 commands in each call. - 7. 5
line(3) moves 3 squares and line(2) moves 2 more: 5. R only turns. 6 multiplies 3 × 2 instead of adding the two lines. - 8. line(5)
From S to G is 5 moves, because the robot already stands on S. line(6) counts the squares instead of the moves and runs off the end; line(4) and line(2) line(2) stop one square short. - 9. 6, then 10
Each call uses its own input: 3 + 3 = 6, then 5 + 5 = 10. 16 adds the inputs first, which the program never does, and 9, then 25 multiplies each number by itself instead of adding it to itself. - 10. line(2) R line(2)
Two squares right, turn right to face down, then two squares down reaches G. line(3) hits the wall in the top row, L turns the robot up and off the grid, and turning first sends it into a wall below S. - 11. square
Four sides with a right turn after each make a square that returns to the start. moveRight describes only the turns, fourSteps hides what happens, and a stair climbs instead of coming back. - 12. Change it to L L
Turning around is a half turn, which is two quarter turns. R is still a quarter turn (the other way), L R turns left and then straight back, so the robot keeps its direction, and F L adds an extra move. - 13. 1
Change the definition once and all 6 calls use the corrected steps. 6 treats each call like copied code, which is exactly the work a function saves. - 14. The body uses 3 instead of n, so the input is ignored.
The fix is Repeat n [F], so the input decides the count. Calling line(3) hides the bug instead of fixing it, a loop can go inside a function, and five calls would move 15 squares. - 15. Reasons include: the program is shorter; it is easier to read because the name says what happens; and if the steps have a bug, you fix it in one place instead of every copy.
Any two clear reasons are fine. The strongest answers mention fixing a bug once. - 16. 16 moves, back at the start facing right
One square is 4 corners × 2 forward moves = 8, so two squares make 16. Each square has four right turns, a full turn, so the robot is back at the start facing right. 8 counts only one call of square. - 17. 3 squares right and 3 squares down
Each zigzag moves 1 right, turns to face down, moves 1 down and turns back to face right. Three calls give 3 right and 3 down. R turns toward the bottom of the grid, not the top, and 6 right assumes every F moves right, ignoring the turns. - 18. Define step [F L F R]; then Repeat 4 [step] (or step step step step).
Trace one call: right, turn up, up, turn back to face right. Ending each call facing right means the next call starts the same way. A different name is fine if it describes the steps.