☰ Year plan · Communication Technology

Functions

Week 8 · Unit 1, Lesson 4 Functions with Parameters in Python · 3 lessons
2

Parameters and arguments: the inputs of a function

ICT Prep 3 Term 1, Unit 1, Lesson 4: pp. 44–52
GoalTell a parameter from an argument and read a function with two parameters.
New words
parameter · the name written in the brackets of the definitionargument · the value given in the callTypeError · shown when a call has the wrong number of argumentsorder · arguments are matched with parameters from left to right
Explanation

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.

Calla getsb getsShows
add_numbers(3, 5)358
add_numbers(10, 20)102030
add_numbers(3)3nothingTypeError
Worked examples
Python
1def add_numbers(a, b):
2    result = a + b
3    print(result)
4
5add_numbers(3, 5)
6add_numbers(10, 20)
Output
8
30
The function add_numbers is defined once and called twice with different arguments. Each call fills a and b again.
Python
1def subtract(a, b):
2    print(a - b)
3
4subtract(10, 4)
5subtract(4, 10)
Output
6
-6
The order of the arguments matters. In the first call a is 10 and b is 4. In the second call a is 4 and b is 10.
Python
1def add_numbers(a, b):
2    result = a + b
3    print(result)
4
5add_numbers(3)
Output
TypeError: the call is missing an argument
Only one argument is given for two parameters, so b has no value. Python stops with a TypeError.
Class activity

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.

Practice
1
In def triangle_area(base, height), which names are the parameters?
2
In print(triangle_area(6, 4)), which value goes into the parameter base?
3
What does subtract(4, 10) show if subtract prints a - b?
4
Why does add_numbers(3) cause an error?