Parameters and arguments: the inputs of a function
Two words are easy to mix up. A parameter is a name in the definition, like a and b in def add_numbers(a, b). An argument is the value in the call, like 3 and 5 in add_numbers(3, 5). The arguments fill the parameters from left to right: a becomes 3 and b becomes 5, so the program shows 8, not 35. The order matters: subtract(10, 4) gives 6 but subtract(4, 10) gives -6. The number must also match. add_numbers(3) gives only one argument for two parameters, and Python stops with a TypeError. Parameters are like empty boxes in the definition. The call decides what goes into the boxes each time.
| Call | a gets | b gets | Shows |
|---|---|---|---|
| add_numbers(3, 5) | 3 | 5 | 8 |
| add_numbers(10, 20) | 10 | 20 | 30 |
| add_numbers(3) | 3 | nothing | TypeError |
1def add_numbers(a, b): 2 result = a + b 3 print(result) 4 5add_numbers(3, 5) 6add_numbers(10, 20)
8 30
1def subtract(a, b): 2 print(a - b) 3 4subtract(10, 4) 5subtract(4, 10)
6 -6
1def add_numbers(a, b): 2 result = a + b 3 print(result) 4 5add_numbers(3)
TypeError: the call is missing an argument
Write a function multiply(a, b) that shows the product. Write two calls and say what each shows. Underline the parameters in the definition and circle the arguments in the calls.