Study CommonsRead · reason · practise

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.

Learning objectiveDefine a function that names a group of commands, call it to run those commands, use an input to change how many times a step happens, trace where a robot ends up, and fix a bug inside a function so every call is fixed at once.

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.

A 4-by-4 coding grid. S is in the bottom-left corner and G is in the top-right corner. Wall squares fill the top-left corner and the right-hand square of each of the bottom two rows. The open squares between S and G form a staircase going up and to the right.GS
A 4-by-4 coding grid. S is in the bottom-left corner and G is in the top-right corner. Wall squares fill the top-left corner and the right-hand square of each of the bottom two rows. The open squares between S and G form a staircase going up and to the right.
  1. 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.
  2. The staircase has three steps, so the chunk happens three times.
  3. Name the chunk: Define stair [F L F R].
  4. 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?

  1. Count the written-out program: 4 commands in each step × 3 steps = 12 commands.
  2. With the function, the main program is just stair stair stair: 3 calls.
  3. 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?

  1. line(3) runs Repeat 3 [F]: 3 squares to the right.
  2. R turns the robot to face down (south). A turn does not move it.
  3. line(2) runs Repeat 2 [F]: 2 squares down.
  4. 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.

An open 6-by-5 coding grid with no walls. S is at the left end of the middle row.S
An open 6-by-5 coding grid with no walls. S is at the left end of the middle row.
  1. Trace: F F moves 2 squares right.
  2. turnAround runs L, which is only a quarter turn. The robot now faces up, not back toward S.
  3. F F moves 2 squares up, away from the start.
  4. Turning around is a half turn: two quarter turns. Change the body to L L (R R also works).
  5. 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

Thinking the robot moves when a function is defined.

Why this fails: Define only names the steps. The steps run when the function is called by name.

Running a function's body only once even when it is called several times.

Why this fails: Each call runs the whole body again. stair stair stair runs F L F R three times.

Ignoring the input.

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.

Fixing the problem in the main program instead of the function.

Why this fails: If the steps inside the function are wrong, patching one call leaves the other calls broken. Fix the definition once.

Choosing a name that does not describe the steps.

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.

Concept

1. What does a function do in a program?

  1. It gives a name to a group of steps so you can run them again by writing the name.
  2. It makes the robot move faster.
  3. It runs the steps only if a condition is true.
  4. It counts how many times a loop runs.
Expand

2. Define hop [F F L]. Program: hop hop. Which shows every command the robot runs?

  1. F F L
  2. F F F F L L
  3. F F L F F L
  4. hop F F L
Count

3. Define jump [F F]. Program: jump R jump. How many squares does the robot move forward?

  1. 2
  2. 3
  3. 5
  4. 4
Define vs call

4. Program: Define spin [R R R R]; F F. How many times does the robot turn?

  1. 4
  2. 0
  3. 8
  4. 1
Robot

5. Define stair [F L F R]. Which program takes the robot from S to G?

  1. stair stair
  2. stair stair stair stair
  3. Repeat 3 [F L F]
  4. stair stair stair
A 4-by-4 coding grid. S is in the bottom-left corner and G is in the top-right corner. Wall squares fill the top-left corner and the right-hand square of each of the bottom two rows. The open squares between S and G form a staircase going up and to the right.GS
A 4-by-4 coding grid. S is in the bottom-left corner and G is in the top-right corner. Wall squares fill the top-left corner and the right-hand square of each of the bottom two rows. The open squares between S and G form a staircase going up and to the right.

Practice

Practice practice

Trace each call, including the input value.

Count commands

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?

  1. 4
  2. 7
  3. 12
  4. 16
Input

7. Define line(n) [Repeat n [F]]. Program: line(3) R line(2). How many squares does the robot move?

  1. 3
  2. 5
  3. 6
  4. 2
Choose the call

8. Define line(n) [Repeat n [F]]. Which call moves the robot from S to G?

  1. line(4)
  2. line(6)
  3. line(5)
  4. line(2) line(2)
A single row of six squares with S on the left end and G on the right end.SG
A single row of six squares with S on the left end and G on the right end.
Trace

9. Define double(x) [say x + x]. Program: double(3) double(5). What does the program say?

  1. 3, then 5
  2. 16
  3. 9, then 25
  4. 6, then 10
Robot

10. Define line(n) [Repeat n [F]]. Which program takes the robot from S to G?

  1. line(2) R line(2)
  2. line(3) R line(2)
  3. line(2) L line(2)
  4. R line(2) L line(2)
A 4-by-3 coding grid. S is at the left end of the top row, followed by two open squares and a wall. In the middle row only the third square is open; the rest are walls. G is the third square of the bottom row.SG
A 4-by-3 coding grid. S is at the left end of the top row, followed by two open squares and a wall. In the middle row only the third square is open; the rest are walls. G is the third square of the bottom row.
Name it

11. Define ??? [F R F R F R F R]. The robot ends where it started. Which name describes the steps best?

  1. moveRight
  2. square
  3. fourSteps
  4. stair

Think

Think practice

Find the bug inside the function and explain your fix.

Fix the bug

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?

  1. Change it to L L
  2. Change it to R
  3. Change it to L R
  4. Change it to F L
Fix once

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?

  1. 6
  2. 1
  3. 7
  4. 0
Input bug

14. Define line(n) [Repeat 3 [F]]. Program: line(5). The robot moves only 3 squares. What is the bug?

  1. The program should call line(3).
  2. Repeat cannot be used inside a function.
  3. line needs to be called five times.
  4. The body uses 3 instead of n, so the input is ignored.
Explain

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.

Functions inside functions

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?

  1. 16 moves, back at the start facing right
  2. 8 moves, back at the start facing right
  3. 16 moves, at the opposite corner of the square
  4. 32 moves, back at the start facing left
Zigzag

17. Define zigzag [F R F L]. The robot faces right. Program: zigzag zigzag zigzag. Where does it end, compared with where it started?

  1. 6 squares right
  2. 3 squares right and 3 squares up
  3. 3 squares right and 3 squares down
  4. 6 squares right and 6 squares down
Write a function

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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.

Related practice