- What a subprogram is, and why programmers split code into smaller parts.
- The difference between a function and a procedure.
- How to write Python 3 functions that return values.
- How to write Python 3 procedures that do not return values.
A subprogram is a named block of code that performs a smaller task inside a larger program.
Instead of writing one long program from top to bottom, programmers split it into subprograms. This makes code easier to:
- read
- test
- reuse
- fix
- update
For example, a quiz program might have separate subprograms to:
- display the menu
- ask a question
- calculate the score
- show the final result
Subprogram
A subprogram is a named section of code that can be run when it is called by another part of the program.
To call a subprogram means to run it by writing its name, followed by brackets.
In Python:
display_menu()
The brackets are still needed even if no data is being passed in.
Call
A call is an instruction that tells the program to run a particular subprogram.
Subprograms can be given data to work with.
A parameter is a variable listed in the subprogram definition. It acts like a placeholder for data that will be supplied later.
An argument is the actual value passed into the subprogram when it is called.
def greet_user(name): # name is a parameter
print("Hello", name)
greet_user("Amina") # "Amina" is an argument
Parameter and argument
A parameter is a variable used in a subprogram definition. An argument is the actual value passed into the subprogram when it is called.
Mixing up parameter and argument
Students often use these words as if they are the same. In exams, be precise: the parameter is in the subprogram definition; the argument is in the call.
The main difference is about whether the subprogram sends a value back to the part of the program that called it.
A function must return a value.
A procedure does not return a value.

Function versus procedure
Use a function when the subprogram needs to produce a result that will be used later. Use a procedure when the subprogram just performs an action, such as printing a message.
A function is a subprogram that returns a value to the code that called it.
In Python, a function is written using def, just like other subprograms, but it uses return to send a value back.
Function
A function is a subprogram that may or may not take parameters, but must return a value.
A function does not have to receive data.
def get_welcome_message():
return "Welcome to the quiz!"
message = get_welcome_message()
print(message)
Here:
get_welcome_message has no parameters
- it returns the string
"Welcome to the quiz!"
- the returned value is stored in
message
A function can also take parameters.
def calculate_total_score(score1, score2):
total = score1 + score2
return total
final_score = calculate_total_score(8, 7)
print(final_score)
Here:
score1 and score2 are parameters
8 and 7 are arguments
- the function returns the total
- the returned value is assigned to
final_score
Writing a function with parameters
Write a function that takes a length and width, calculates the area of a rectangle, and returns the result.
-
Decide what data the function needs to receive. It needs length and width, so these become parameters.
-
Decide what value the function must send back. The result is the area, so the function must return area.
-
Write the function using return, because the calculated value needs to be used by the calling code.
def calculate_rectangle_area(length, width):
area = length * width
return area
- Call the function and store the returned value in a variable.
rectangle_area = calculate_rectangle_area(6, 4)
print(rectangle_area)
The return statement sends a value back from a function to the line of code that called it.
For example:
def double_number(number):
return number * 2
answer = double_number(5)
The function call double_number(5) is replaced by the returned value, so answer stores 10.
How to spot a function
If the subprogram call is used in an assignment, calculation, comparison, or output statement, it is probably a function, because a value is being returned and used.
Printing instead of returning
If a question asks you to write a function, do not just use print. A function must use return to send a value back.
A procedure is a subprogram that performs a task but does not return a value.
In Python, procedures are also written using def, but they do not return a useful value.
Procedure
A procedure is a subprogram that may or may not take parameters, but does not return a value.
A procedure can perform an action without receiving any data.
def display_menu():
print("1. Start quiz")
print("2. View scores")
print("3. Quit")
display_menu()
This procedure displays text on the screen. It does not calculate and return a value.
A procedure can take parameters if it needs data to carry out its action.
def display_score(name, score):
print(name, "scored", score)
display_score("Amina", 15)
Here, the procedure receives name and score, then prints a message.
Writing a procedure with parameters
Write a procedure that takes a username and displays a welcome message.
-
Decide whether a returned value is needed. The task is only to display a message, so this should be a procedure.
-
Decide what data the procedure needs. It needs the username, so username becomes a parameter.
-
Write the procedure so it performs the action using print, with no returned value.
def display_welcome(username):
print("Welcome,", username)
- Call the procedure on its own line, because there is no returned value to store.
display_welcome("Sam")
Python and None
In Python, a subprogram with no return statement technically returns None. For GCSE, treat this as a procedure because it does not return a useful value for the program to use.
| Feature | Function | Procedure |
|---|
| Takes parameters? | May or may not | May or may not |
| Returns a value? | Yes, must return a value | No |
| Common purpose | Calculate or produce a result | Perform an action |
| Python keyword used to define it | def | def |
| Usually called how? | In an assignment or expression | On its own line |
When deciding which one to write, ask:
- Does the subprogram need to give a value back?
- Will the calling code need to store, compare, print, or calculate with the result?
- Or does the subprogram simply perform an action?
Use a function for a result.
Use a procedure for an action.
Choosing the correct subprogram
A program needs one subprogram to calculate the average score, and another subprogram to display a certificate.
- The average score is a value that the program may need to store or compare later, so this should be a function.
def calculate_average(score1, score2):
average = (score1 + score2) / 2
return average
- The certificate is just displayed to the user, so this should be a procedure.
def display_certificate(name):
print("Certificate awarded to", name)
- The main program can use the returned average from the function, then call the procedure to display the certificate.
average_score = calculate_average(10, 8)
print("Average score:", average_score)
display_certificate("Amina")
Real programs often use both.
A function might calculate a value, then a procedure might display it.
def calculate_discounted_price(price, discount):
discounted_price = price - discount
return discounted_price
def display_price(price):
print("Final price:", price)
final_price = calculate_discounted_price(20, 5)
display_price(final_price)
In this example:
calculate_discounted_price is a function because it returns a value
display_price is a procedure because it only prints a value
final_price stores the returned value from the function
Tracing a returned value
Trace this code:
def add_bonus(score):
new_score = score + 5
return new_score
def display_result(result):
print("Result:", result)
player_score = add_bonus(12)
display_result(player_score)
-
The call add_bonus(12) passes the argument 12 into the parameter score.
-
Inside the function, new_score is calculated as 12 + 5, giving 17.
-
The function returns 17, so player_score is assigned the value 17.
-
The procedure display_result(player_score) receives 17 and prints Result: 17, but it does not return a value.
Use meaningful names so the purpose of each subprogram is obvious.
Good function names often describe the value being calculated:
calculate_total()
get_username()
find_largest_number()
Good procedure names often describe the action being performed:
display_menu()
print_receipt()
save_score()
Also remember that Python uses indentation to show which lines belong inside the subprogram.
def display_message():
print("This line is inside the procedure")
print("This line is outside the procedure")
Name clue
Function names often sound like they are getting, calculating, or finding something. Procedure names often sound like they are displaying, printing, or updating something.
In the exam
-
If asked to write a function, include a return statement and make sure the returned value is used correctly.
-
If asked to write a procedure, do not return a value; call it on its own line to perform its action.
-
Check whether parameters are required: if the subprogram needs data from outside, include parameters in the brackets.
Check yourself
- What is the difference between a parameter and an argument?
- Why would
calculate_total(score1, score2) probably be a function?
- Write one example of a procedure that takes no parameters.