How do functions package code with parameters and return values, and why do they help?
Define and call Python functions with parameters and return values, and explain the benefits of using functions
A focused answer to the O-Level Computing point on Python functions. Defining functions with def, passing parameters, returning values, the difference between a parameter and an argument, and why functions make programs reusable.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
SEAB wants you to define and call Python functions that take parameters and return values, and to explain why functions are useful. The central idea is that a function packages a task under a name, so it can be called many times, tested on its own, and changed in one place, which keeps a program short, clear and reliable.
The answer
Defining a function
You define a function with def, a name, and a list of parameters in brackets. The indented block below is the function body:
def greet(name):
print("Hello", name)
This defines greet but does not run it yet. The function runs only when it is called.
Calling a function and passing arguments
You call a function by writing its name with arguments in brackets:
greet("Mei") # prints: Hello Mei
The argument "Mei" is passed in and used as the parameter name inside the function.
Parameters versus arguments
- A parameter is the name in the definition, a placeholder:
nameindef greet(name). - An argument is the actual value supplied at the call:
"Mei"ingreet("Mei").
Returning a value
A function can send a result back with return. The returned value can be stored or used in an expression:
def add(a, b):
return a + b
total = add(2, 3) # total is 5
A function with no return gives back None by default. Use return when the caller needs the result; use a plain print only when the job is to display something.
Why functions help
Functions make programs better because they:
- Avoid repetition: write the logic once, call it many times.
- Are reusable: the same function works in many programs.
- Are testable: you can check a function alone with known inputs.
- Are easier to maintain: a change is made in one place.
Examples in context
Example 1. A reusable tax calculation. A program defines tax(amount) once and calls it for every sale in a loop. Because the rule lives in one function, a change to the tax rate is made in a single place, and the function can be tested on known amounts before use.
Example 2. Validating input. A valid_age(age) function returns True or False, and the main program calls it wherever an age is entered. The same checked logic guards several inputs without repeating the conditions, and a fix to the rule updates every use at once.
Try this
Q1. State the difference between a parameter and an argument. [2 marks]
- Cue. A parameter is the placeholder name in the function definition; an argument is the actual value passed when the function is called.
Q2. Write a function double(n) that returns twice its input. [2 marks]
- Cue.
def double(n): return n * 2.
Q3. Give one benefit of writing code as a function rather than repeating it. [2 marks]
- Cue. It can be reused by calling it many times, so the logic is written and maintained in one place (testability and readability are also valid).
Exam-style practice questions
Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
Original6 marksWrite a Python function `area_of_rectangle(width, height)` that returns the area, then write code that calls it for a rectangle by and prints the result. Label the parameters and the arguments in your answer.Show worked answer →
Define the function with def, return the calculation, then call it:
def area_of_rectangle(width, height):
return width * height
result = area_of_rectangle(5, 3)
print(result)
The parameters are width and height (the names in the definition). The arguments are and (the actual values passed in the call). The function returns , so result is and the program prints .
Markers reward def with two parameters, a return of width * height, a call with the arguments and , and correctly labelling parameters versus arguments.
Original5 marks(a) Explain the difference between a function that returns a value and one that only prints. (b) Give two benefits of writing part of a program as a function rather than repeating the code.Show worked answer →
(a) A function that returns a value sends a result back to the caller, so it can be stored in a variable or used in an expression (for example total = add(2, 3)). A function that only prints displays text on the screen but gives nothing back, so its result cannot be reused; calling it where a value is expected gives None.
(b) Benefits include: the code can be reused by calling it many times without repeating it; it can be tested in isolation with known inputs; it makes the program shorter and easier to read; and a change to the logic is made in one place. Any two of these are acceptable.
Markers reward return giving back a usable value (versus print only displaying), and two valid benefits such as reuse, testability, readability or single-point maintenance.
Related dot points
- Declare and use Python variables, identify the core data types (int, float, str, bool), and convert between them
A focused answer to the O-Level Computing point on Python variables. Assigning variables, the core data types int, float, str and bool, reading input as a string, and converting between types with int(), float() and str().
- Use Python selection with if, elif and else, build conditions with comparison and logical operators, and order branches correctly
A focused answer to the O-Level Computing point on Python selection. Using if, elif and else, comparison operators, combining conditions with and, or and not, and ordering branches so each value falls into the right one.
- Use Python for loops with range and while loops with a condition, including accumulators and avoiding infinite loops
A focused answer to the O-Level Computing point on Python loops. Using for with range for a known count, while with a condition for an unknown count, building accumulators, and avoiding infinite loops.
- Use Python lists and strings, including indexing, length, looping over items, and basic operations like append and slicing
A focused answer to the O-Level Computing point on Python lists and strings. Creating lists, zero-based indexing, finding length with len(), looping over items, appending, and basic string operations and slicing.
- Use a trace table to follow variable values through an algorithm, and choose normal, boundary and invalid test data
A focused answer to the O-Level Computing point on tracing and testing. Building a trace table to track variables through an algorithm, and choosing normal, boundary and invalid test data to find errors.