Study CommonsRead · reason · practise

Home / Reasoning / Functions with Inputs: One Function, Different Numbers

Reasoning & Coding · Coding

Functions with Inputs: One Function, Different Numbers

Trace functions that take inputs: swap in each call's number, predict what the robot does or says, keep two inputs in order and fix a wrong call.

Learning objectiveTrace a function that takes one or two inputs by replacing each input name with the value in the call, predict the moves or outputs of several calls, keep two inputs in the right order, and decide whether a bug is in the call or in the function's body.

The idea

A recipe might say "bake for 20 minutes", but a better recipe card says "bake for the time on the chart", so one card works for small and large muffins. In the last lesson, functions named a group of steps. Today the steps stay the same, but each call can hand the function a different number, and the result changes.

In Define line(n) [Repeat n [F]], the n in brackets is the input. Programmers also call it a parameter. It is a name that stands for a number we do not know yet. In the call line(4), the value 4 is handed in, and during that call every n in the body becomes 4. The next call, line(2), starts fresh with n = 2.

An input can be used more than once in the body. Define echo(n) [Repeat n [say n]] uses n twice: echo(3) says 3, three times. The body can also do arithmetic with the input, such as say n × 3.

A function can take two inputs, separated by a comma: Define path(a, b) [Repeat a [F] R Repeat b [F]]. Order matters. In path(3, 2), the first value goes to a and the second to b, so the robot goes 3 squares, turns right, then goes 2. path(2, 3) is a different trip.

To predict what a program does, trace each call: write the call, the value of each input, and what happens. When something goes wrong, ask where the bug is. If the body is right but the call hands in the wrong number, fix the call. If the body ignores or misuses its input, fix the function once.

As before, F moves the robot forward one square, L and R turn it a quarter turn without moving, 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 · One function, three calls

Define triple(n) [say n × 3]. Program: triple(2) triple(5) triple(10). What does the program say?

  1. Make a trace table with one row for each call: call, value of n, what is said.
  2. triple(2): n = 2, so it says 2 × 3 = 6.
  3. triple(5): n = 5, so it says 5 × 3 = 15.
  4. triple(10): n = 10, so it says 10 × 3 = 30. Each call uses only its own value.

It says 6, then 15, then 30.

Example 2 · A square of any size

Define square(size) [Repeat 4 [Repeat size [F] R]]. Compare square(2) and square(3).

  1. In square(2), size = 2. Each side is Repeat 2 [F], then R. Four sides: 4 × 2 = 8 forward moves.
  2. In square(3), size = 3. Four sides of 3 moves: 4 × 3 = 12 forward moves.
  3. Both calls make four right turns, a full turn, so the robot ends where it started, facing right.

square(2) moves 8 squares and square(3) moves 12. Both draw a square and finish back at the start facing right.

Example 3 · Two inputs, order matters

Define path(a, b) [Repeat a [F] R Repeat b [F]]. Which call takes the robot from S to G: path(3, 2) or path(2, 3)?

An open 4-by-3 coding grid with no walls. S is in the top-left corner and G is in the bottom-right corner.SG
An open 4-by-3 coding grid with no walls. S is in the top-left corner and G is in the bottom-right corner.
  1. path(3, 2): a = 3 and b = 2. The robot moves 3 squares right to the top-right corner, turns right to face down, then moves 2 squares down to G.
  2. path(2, 3): a = 2 and b = 3. The robot moves 2 right, turns to face down, and tries to move 3 down, but there are only 2 rows below, so it runs off the bottom of the grid.
  3. The first value always goes to the first input name, a.

path(3, 2) reaches G. Swapping the inputs gives a different path.

Example 4 · Fix the call or fix the function?

Define hop(n) [Repeat n [F F]]. Ravi wants the robot to move 6 squares, so he writes hop(6). The robot moves 12 squares. What should he change?

  1. Trace: hop(6) runs Repeat 6 [F F], which is 6 × 2 = 12 moves.
  2. Each hop moves 2 squares, so n counts hops, not squares.
  3. If hop is meant to count hops, the function is fine and the call is wrong: 6 squares is 3 hops, so call hop(3).
  4. If the function was meant to move n squares, its body is wrong and should be Repeat n [F]. The name and the plan decide which fix is right.

With this hop function, the right call is hop(3), which moves 3 × 2 = 6 squares.

Example 5 · A variable as the input

Define double(n) [say n + n]. Program: Set x to 2; double(x); Change x by 3; double(x). What is said?

  1. x starts at 2. The call double(x) hands in the value of x, so n = 2 and it says 4.
  2. Change x by 3 makes x = 5.
  3. The second double(x) hands in 5, so n = 5 and it says 10.

It says 4, then 10. The call is written the same way twice, but the value handed in has changed.

Common mistakes

Leaving the input name in the steps instead of the number.

Why this fails: During a call, every copy of the input name stands for the value in that call. In jump(4), Repeat n means Repeat 4.

Carrying a value over from the last call.

Why this fails: Each call gets its own value. After line(3), the call line(2) uses n = 2, not 3 or 5.

Swapping two inputs.

Why this fails: The first value goes to the first input name and the second to the second. path(1, 4) and path(4, 1) are different trips.

Counting squares instead of moves when choosing the input.

Why this fails: The robot already stands on S. Count the moves it must make, which is one less than the number of squares including S.

Changing the function when only the call is wrong.

Why this fails: If the body correctly uses its input, hand in the right number instead. Changing the body to a fixed number breaks every other call.

Foundation

Foundation practice

Swap the call's number in for the input name, then trace.

Concept

1. In Define line(n) [Repeat n [F]], what is n?

  1. The name of the function
  2. A command that moves the robot
  3. The input: a name that stands for the number given in each call
  4. The number of times the function may be called
Swap in

2. Define jump(n) [Repeat n [F] L]. Which steps does the call jump(4) run?

  1. Repeat 4 [F] L
  2. Repeat n [F] L
  3. F L
  4. Repeat 4 [F L]
Predict

3. Define add5(n) [say n + 5]. Program: add5(1) add5(10). What does the program say?

  1. 15
  2. 1, then 10
  3. 5, then 5
  4. 6, then 15
Count moves

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

  1. 3
  2. 9
  3. 24
  4. 4
Choose the input

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

  1. line(4)
  2. line(3)
  3. line(2)
  4. line(5)
A single row of five squares: S on the left, two open squares, G, then a wall on the right end.SG
A single row of five squares: S on the left, two open squares, G, then a wall on the right end.

Practice

Practice practice

Trace each call with its own value. Keep two inputs in order.

Trace table

6. Define triple(n) [say n × 3]. Program: triple(4) triple(0) triple(7). What is said?

  1. 12, then 0, then 21
  2. 12, then 3, then 21
  3. 7, then 3, then 10
  4. 12, then 21
Square

7. Define square(size) [Repeat 4 [Repeat size [F] R]]. Program: square(3). How many forward moves does the robot make?

  1. 3
  2. 4
  3. 7
  4. 12
Two calls

8. Define square(size) [Repeat 4 [Repeat size [F] R]]. Program: square(1) square(2). How far does the robot move, and where does it finish?

  1. 8 moves, back at the start facing right
  2. 3 moves, back at the start facing right
  3. 12 moves, back at the start facing right
  4. 12 moves, 3 squares to the right of the start
Two inputs

9. Define path(a, b) [Repeat a [F] R Repeat b [F]]. Program: path(1, 4). Where does the robot end, compared with where it started?

  1. 1 square right and 4 squares down
  2. 4 squares right and 1 square down
  3. 5 squares right
  4. 1 square right and 4 squares up
Choose the call

10. Define path(a, b) [Repeat a [F] R Repeat b [F]]. Which call takes the robot from S to G?

  1. path(3, 2)
  2. path(2, 3)
  3. path(2, 2)
  4. path(3, 3)
A 5-by-4 coding grid. S is at the left end of the top row, followed by two open squares and two walls. In the second and third rows only the middle square is open. In the bottom row all squares are open and G is the middle square.SG
A 5-by-4 coding grid. S is at the left end of the top row, followed by two open squares and two walls. In the second and third rows only the middle square is open. In the bottom row all squares are open and G is the middle square.
Variable input

11. Define double(n) [say n + n]. Program: Set x to 2; double(x); Change x by 3; double(x). What is said?

  1. 4, then 7
  2. 4, then 10
  3. 2, then 5
  4. 4, then 4
Input used twice

12. Define echo(n) [Repeat n [say n]]. What does echo(3) say?

  1. 3
  2. 1, 2, 3
  3. n, n, n
  4. 3, 3, 3

Think

Think practice

Decide whether the bug is in the call or in the function, and explain.

Wrong input

13. Define line(n) [Repeat n [F]]. Mia counts 5 squares from S to G and writes line(5). The robot crashes into the wall. Which fix is best?

  1. Change the call to line(4).
  2. Change the function to Repeat 4 [F].
  3. Change the call to line(6).
  4. Add line(1) at the end.
A single row of six squares: S on the left, three open squares, G, then a wall on the right end.SG
A single row of six squares: S on the left, three open squares, G, then a wall on the right end.
Swapped inputs

14. Define path(a, b) [Repeat a [F] R Repeat b [F]]. Ana wants the robot to go 4 squares right, then 1 square down. She writes path(1, 4). What does the robot do instead?

  1. It goes 4 squares right, then 1 square down.
  2. It goes 5 squares right.
  3. It goes 1 square right, then 4 squares down.
  4. It goes 4 squares down, then 1 square right.
Bug in the body

15. Define triple(n) [say n + 3]. The call triple(4) should say 12, but it says 7. What is the bug?

  1. The call should be triple(12).
  2. The call should be triple(9).
  3. A function cannot use say.
  4. The body adds 3 instead of multiplying by 3; it should say n × 3.
Fix the call

16. Define hop(n) [Repeat n [F F]]. Which call moves the robot exactly 6 squares?

  1. hop(6)
  2. hop(12)
  3. hop(3)
  4. hop(4)
Explain

17. Why is one function line(n) better than three separate functions line2, line3 and line4? Give two reasons.

Challenge

Challenge practice

Inputs pass from one function to another. Trace each layer carefully.

Function inside a function

18. Define line(n) [Repeat n [F]]. Define square(size) [Repeat 4 [line(size) R]]. Define twoSquares(k) [square(k) square(k + 1)]. Program: twoSquares(2). How many forward moves happen?

  1. 5
  2. 16
  3. 20
  4. 24
Work backwards

19. Define triple(n) [say n × 3]. A call to triple said 27. What number was handed in?

  1. 24
  2. 9
  3. 30
  4. 81
Growing input

20. Define line(n) [Repeat n [F]]. Program: Set s to 1; Repeat 3 [line(s) R Change s by 1]. How many squares does the robot move, and where does it end?

  1. 3 moves; back at the start
  2. 6 moves; 6 squares right of the start
  3. 9 moves; 2 squares left and 2 squares down from the start, facing up
  4. 6 moves; 2 squares left and 2 squares down from the start, facing up
Write a function

21. Write a function climb(n) that climbs n steps of a staircase going up and to the right, one step being F L F R. Then write the call that climbs 4 steps.

Answer key

  1. 1. The input: a name that stands for the number given in each call
    n is the input (parameter). Each call fills it with a number. The function's name is line, the moving command is F, and nothing limits how many times line can be called.
  2. 2. Repeat 4 [F] L
    Put 4 wherever n appears: Repeat 4 [F] L. Repeat n leaves the name unfilled, F L forgets the repeat, and Repeat 4 [F L] moves the turn inside the loop, which would turn after every move.
  3. 3. 6, then 15
    Each call adds 5 to its own input: 1 + 5 = 6 and 10 + 5 = 15. 15 alone forgets the first call, 1, then 10 only repeats the inputs, and 5, then 5 ignores them.
  4. 4. 9
    The three calls move 2 + 3 + 4 = 9 squares. 3 counts the calls, 24 multiplies the inputs, and 4 uses only the last call.
  5. 5. line(3)
    The robot already stands on S, so it needs 3 moves to reach G. line(4) counts S as a move and hits the wall, line(2) stops one square short, and line(5) crashes too.
  6. 6. 12, then 0, then 21
    4 × 3 = 12, 0 × 3 = 0 and 7 × 3 = 21. 3 for the middle call treats 0 × 3 as 3, 7, 3, 10 adds 3 instead of multiplying, and leaving out a number forgets that triple(0) still runs and says 0.
  7. 7. 12
    Each of the 4 sides is 3 moves: 4 × 3 = 12. 3 is only one side, 4 counts the sides, and 7 adds 4 + 3 instead of multiplying.
  8. 8. 12 moves, back at the start facing right
    square(1) is 4 × 1 = 4 moves and square(2) is 4 × 2 = 8 moves: 12 in all. Each square ends where it began, facing right. 8 counts only the second call, 3 adds the inputs, and the robot does not end to the right because each square closes up.
  9. 9. 1 square right and 4 squares down
    The first value goes to a: 1 square right. R turns the robot to face down, and b = 4 sends it 4 squares down. Swapping gives 4 right and 1 down, 5 right ignores the turn, and a right turn from facing east points down, not up.
  10. 10. path(2, 3)
    Two squares right reaches the open column, R turns the robot down, and 3 moves down land on G. path(3, 2) and path(3, 3) hit the wall on the third move right, and path(2, 2) stops one square above G.
  11. 11. 4, then 10
    The first call hands in 2 and says 4. x becomes 5, so the second call hands in 5 and says 10. 4, then 7 adds 3 to the first answer instead of to x, 2, then 5 only says x, and 4, then 4 forgets that x changed.
  12. 12. 3, 3, 3
    Both copies of n become 3: Repeat 3 [say 3] says 3 three times. 3 alone ignores the repeat, 1, 2, 3 counts up although n never changes, and n, n, n leaves the name in place of the value.
  13. 13. Change the call to line(4).
    The robot starts on S, so it needs only 4 moves. The function is fine, so fix the call. Changing the body to Repeat 4 would break every other call to line, and line(6) or an extra line(1) moves even further into the wall.
  14. 14. It goes 1 square right, then 4 squares down.
    The first value fills a, so a = 1 and b = 4. The fix is path(4, 1). 4 right then 1 down is what she wanted, not what she wrote; 5 right ignores the turn; and the function always moves right before it turns down.
  15. 15. The body adds 3 instead of multiplying by 3; it should say n × 3.
    4 + 3 = 7, but triple means 4 × 3 = 12. The body is wrong, so fix it once. triple(12) would say 15 with this body, triple(9) would say 12 only by hiding the bug, and say works fine inside a function.
  16. 16. hop(3)
    Each hop is 2 moves, so 3 hops make 6 squares. hop(6) moves 12 squares, hop(12) moves 24 and hop(4) moves 8.
  17. 17. Reasons include: one function works for any length, even ones you have not thought of yet; there is less code to write and read; and a bug only needs fixing in one place.
    Any two clear reasons are fine. The strongest answers say that the input lets the same steps work for many different numbers.
  18. 18. 20
    k = 2, so the calls are square(2) and square(3): 8 + 12 = 20 moves. 5 adds the two side lengths, 2 + 3, 16 uses square(2) twice, ignoring the + 1, and 24 uses square(3) twice.
  19. 19. 9
    9 × 3 = 27, so the input was 9. 24 subtracts 3, 30 adds 3, and 81 multiplies 27 by 3 instead of dividing.
  20. 20. 6 moves; 2 squares left and 2 squares down from the start, facing up
    The calls are line(1), line(2) and line(3): 6 moves. Trace: 1 right, turn to face down, 2 down, turn to face left, 3 left, turn to face up. That ends 2 squares left of the start and 2 down. 3 moves uses s = 1 every time, 6 right ignores the turns, and 9 multiplies 3 × 3.
  21. 21. Define climb(n) [Repeat n [F L F R]]; then climb(4).
    The input goes in the Repeat, so climb(2) climbs 2 steps and climb(4) climbs 4. Each step ends facing right, so the next step starts the same way. A different name is fine if it describes the steps.

Related practice