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.
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?
- Make a trace table with one row for each call: call, value of n, what is said.
- triple(2): n = 2, so it says 2 × 3 = 6.
- triple(5): n = 5, so it says 5 × 3 = 15.
- 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).
- In square(2), size = 2. Each side is Repeat 2 [F], then R. Four sides: 4 × 2 = 8 forward moves.
- In square(3), size = 3. Four sides of 3 moves: 4 × 3 = 12 forward moves.
- 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)?
- 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.
- 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.
- 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?
- Trace: hop(6) runs Repeat 6 [F F], which is 6 × 2 = 12 moves.
- Each hop moves 2 squares, so n counts hops, not squares.
- 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).
- 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?
- x starts at 2. The call double(x) hands in the value of x, so n = 2 and it says 4.
- Change x by 3 makes x = 5.
- 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
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.
Why this fails: Each call gets its own value. After line(3), the call line(2) uses n = 2, not 3 or 5.
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.
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.
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.
1. In Define line(n) [Repeat n [F]], what is n?
- The name of the function
- A command that moves the robot
- The input: a name that stands for the number given in each call
- The number of times the function may be called
2. Define jump(n) [Repeat n [F] L]. Which steps does the call jump(4) run?
- Repeat 4 [F] L
- Repeat n [F] L
- F L
- Repeat 4 [F L]
3. Define add5(n) [say n + 5]. Program: add5(1) add5(10). What does the program say?
- 15
- 1, then 10
- 5, then 5
- 6, then 15
4. Define line(n) [Repeat n [F]]. Program: line(2) line(3) line(4). How many squares does the robot move?
- 3
- 9
- 24
- 4
5. Define line(n) [Repeat n [F]]. Which call moves the robot from S to G without hitting the wall?
- line(4)
- line(3)
- line(2)
- line(5)
Practice
Practice practice
Trace each call with its own value. Keep two inputs in order.
6. Define triple(n) [say n × 3]. Program: triple(4) triple(0) triple(7). What is said?
- 12, then 0, then 21
- 12, then 3, then 21
- 7, then 3, then 10
- 12, then 21
7. Define square(size) [Repeat 4 [Repeat size [F] R]]. Program: square(3). How many forward moves does the robot make?
- 3
- 4
- 7
- 12
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?
- 8 moves, back at the start facing right
- 3 moves, back at the start facing right
- 12 moves, back at the start facing right
- 12 moves, 3 squares to the right of the start
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 square right and 4 squares down
- 4 squares right and 1 square down
- 5 squares right
- 1 square right and 4 squares up
10. Define path(a, b) [Repeat a [F] R Repeat b [F]]. Which call takes the robot from S to G?
- path(3, 2)
- path(2, 3)
- path(2, 2)
- path(3, 3)
11. Define double(n) [say n + n]. Program: Set x to 2; double(x); Change x by 3; double(x). What is said?
- 4, then 7
- 4, then 10
- 2, then 5
- 4, then 4
12. Define echo(n) [Repeat n [say n]]. What does echo(3) say?
- 3
- 1, 2, 3
- n, n, n
- 3, 3, 3
Think
Think practice
Decide whether the bug is in the call or in the function, and explain.
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?
- Change the call to line(4).
- Change the function to Repeat 4 [F].
- Change the call to line(6).
- Add line(1) at the end.
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?
- It goes 4 squares right, then 1 square down.
- It goes 5 squares right.
- It goes 1 square right, then 4 squares down.
- It goes 4 squares down, then 1 square right.
15. Define triple(n) [say n + 3]. The call triple(4) should say 12, but it says 7. What is the bug?
- The call should be triple(12).
- The call should be triple(9).
- A function cannot use say.
- The body adds 3 instead of multiplying by 3; it should say n × 3.
16. Define hop(n) [Repeat n [F F]]. Which call moves the robot exactly 6 squares?
- hop(6)
- hop(12)
- hop(3)
- hop(4)
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.
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?
- 5
- 16
- 20
- 24
19. Define triple(n) [say n × 3]. A call to triple said 27. What number was handed in?
- 24
- 9
- 30
- 81
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?
- 3 moves; back at the start
- 6 moves; 6 squares right of the start
- 9 moves; 2 squares left and 2 squares down from the start, facing up
- 6 moves; 2 squares left and 2 squares down from the start, facing up
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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.