Functions
Answers are for parents and teachers.
1
In the lines below, name the parameters and the arguments.
Python
1def triangle_area(base, height): 2 return 0.5 * base * height 3 4print(triangle_area(6, 4))
The parameters are base and height (in the definition). The arguments are 6 and 4 (in the call).
2
Predict the two lines shown by subtract(10, 4) and subtract(4, 10).
Python
1def subtract(a, b): 2 print(a - b) 3 4subtract(10, 4) 5subtract(4, 10)
6 and -6. The order of the arguments matters.
3
What do the two lines show?
Python
1def celsius_to_f(c): 2 return c * 9 / 5 + 32 3 4print(celsius_to_f(25)) 5print(celsius_to_f(100))
77.0 and 212.0. The function returns the value and print shows it.
4
A price after a 10 percent discount is price * 0.9. Write a function discounted(price) that returns it. What does discounted(200) give?
def discounted(price): then return price * 0.9. It gives 180.0.
5
What do the two lines show, and why does each return end the function?
Python
1def bigger(a, b): 2 if a > b: 3 return a 4 else: 5 return b 6 7print(bigger(8, 3)) 8print(bigger(2, 9))
8 and 9. Only one branch runs each time, and return hands back its value at once.