AQA 7517, section 4.1.1.12
Parameters carry data into a subroutine. A return value carries a result back out to the code that called it. A subroutine that does this is a function.
The return statement
The return statement ends the subroutine and sends a value back. The call is then replaced by that value, so you can store it, print it or use it in a bigger expression.
def double(n):
return n * 2
result = double(8)
print(result) # 16
print(double(3) + 1) # 7
Execution leaves the function the moment return runs, so any lines after it are not reached.
def sign(n):
if n < 0:
return "negative"
if n > 0:
return "positive"
return "zero"
Procedures versus functions
| Returns a value? | Typical use | |
|---|---|---|
| Procedure | No | Displaying output, changing something |
| Function | Yes | Calculating and giving back a result |
def show_total(price, qty): # procedure: prints, returns nothing
print("Total:", price * qty)
def total(price, qty): # function: returns the answer
return price * qty
order = total(4.50, 3)
Returning a value is usually more flexible, because the calling code decides what to do with the result. It can print it, store it, or use it in another calculation.
Watch out: print and return are not the same. print shows something on the screen. return sends a value back to the program. A function that only prints can't have its result used in later calculations, and in Python it gives back None.
Returning more than one value
A subroutine can return several values. In Python you can return them together as a tuple and then unpack them:
def min_max(numbers):
return min(numbers), max(numbers)
lowest, highest = min_max([4, 9, 2, 7])
print(lowest, highest) # 2 9
Other languages might do this by returning a record or an array, or by using parameters passed by reference.
Worked example
Task: write a function that takes a list of marks and returns the average, then use it to describe a result.
def average(marks):
return sum(marks) / len(marks)
scores = [62, 71, 58, 80]
avg = average(scores)
if avg >= 65:
print("Good:", avg)
else:
print("Keep going:", avg)
The function does only one job, which is to calculate and return. The decision about what to display is made by the calling code. That keeps average reusable. It could be used for temperatures, prices or anything else.
Here sum(marks) is 271 and len(marks) is 4, so the function returns 67.75, and the message printed is Good: 67.75.
Exam tip: when you write a function in an exam, check that every path through it reaches a return. A common slip is to compute the answer and forget to return it.
Check yourself
What does a function's return statement do?
It ends the function and sends a value back to the code that called it.
What is the difference between printing a value and returning it?
Printing displays the value to the user. Returning passes the value back to the calling code so it can be used further. A function can do both, or either.
What does this print? def f(x): return x + 1 then print(f(f(1)))
3. The inner call f(1) returns 2, and then f(2) returns 3.
How can a Python function return two values?
By returning them together, for example return a, b, which creates a tuple. The caller can unpack them with x, y = function().