AQA 7517, section 4.1.1.11
A subroutine often needs data to work with. A parameter is a way of passing data into a subroutine when it is called, so the same subroutine can work with different values each time.
Parameters and arguments
- A parameter is the name listed in the subroutine's definition. It acts as a variable inside the subroutine.
- An argument is the actual value you give when you call it.
def add(a, b): # a and b are the parameters
return a + b
print(add(3, 4)) # 3 and 4 are the arguments: 7
print(add(10, 25)) # 35
When add(3, 4) runs, a receives 3 and b receives 4. The values are matched by position: first argument to first parameter, and so on. The number of arguments needs to match the number of parameters.
Without parameters, the subroutine could only work on fixed values, or would depend on other variables elsewhere in the program. With them, it is self-contained and reusable.
Subroutine interfaces
The interface of a subroutine is everything another part of the program needs to know to use it:
- its name;
- its parameters (how many, in what order, and of what type);
- the type of value it returns, if any.
The programmer using the subroutine only needs the interface. They don't need to know how it works inside. This is a form of abstraction, and it lets one person write a subroutine while another uses it.
def calculate_bmi(weight_kg, height_m):
"""Return the body mass index for the given weight and height."""
return weight_kg / (height_m ** 2)
bmi = calculate_bmi(70, 1.75)
Anyone calling calculate_bmi needs to know it takes two numbers (weight in kg, then height in metres) and returns a number. They don't need to see the formula.
Passing by value and by reference
Languages differ in what a parameter receives:
- Pass by value: the subroutine gets a copy of the value. Changing the parameter inside the subroutine doesn't change the original variable.
- Pass by reference: the subroutine gets a reference to the original data, so changes made inside the subroutine are visible outside.
def add_one(n):
n = n + 1 # changes only the local copy
def add_item(items):
items.append("new") # changes the original list
x = 5
add_one(x)
print(x) # 5, unchanged
stock = ["pen"]
add_item(stock)
print(stock) # ['pen', 'new'], changed
Python passes a reference to the object. Because numbers can't be changed in place, the number example behaves like pass by value. A list can be changed in place, so changes are visible outside.
Worked example
Task: write a subroutine that prints a line of a chosen character, of a chosen length, and use it to draw a box top and bottom.
def line(char, length):
print(char * length)
line("*", 10)
print("* Hello *")
line("*", 10)
The output is:
**********
* Hello *
**********
The subroutine has two parameters, char and length, so one definition works for any character and any width. Calling line("-", 25) would draw a different line with no change to the code.
Exam tip: be clear about the words. Parameters are the names in the definition, and arguments are the values in the call. In practice examiners often accept either for "values passed in", but describing the purpose, "to pass data into the subroutine", is the key point.
Check yourself
What is the purpose of a parameter?
To pass data into a subroutine, so it can work with different values each time it is called.
In def area(w, h) and the call area(3, 5), which are the parameters and which are the arguments?
w and h are the parameters. 3 and 5 are the arguments.
What does the interface of a subroutine tell you?
Its name, its parameters (how many, in what order, and what types) and what it returns. It tells you how to use it without showing how it works.
What is the difference between passing by value and passing by reference?
By value passes a copy, so the original isn't changed. By reference passes a reference to the original data, so changes inside the subroutine affect the original.