AQA 7517, section 4.1.1.10
A subroutine is a named block of code that sits "out of line" with the main program. It is only run when it is called, which you do by writing its name in a program statement. When it finishes, the program carries on from just after the call.
There are two kinds:
- A procedure carries out a task but doesn't give a value back.
- A function carries out a task and returns a value to the place where it was called.
Python defines both with def. The difference is whether there's a return with a value.
def show_menu(): # procedure
print("1. Play")
print("2. Quit")
def square(n): # function
return n * n
show_menu() # call the procedure
answer = square(6) # call the function, use the value
print(answer) # 36
How a call works
- The program reaches the call, such as
show_menu(). - It jumps to the subroutine's code and runs it.
- When the subroutine ends, control goes back to the line just after the call.
The subroutine can be called as many times as you like, from anywhere in the program.
def greet():
print("Welcome to the quiz")
greet()
print("Let's start")
greet()
Advantages of using subroutines
- Avoid repetition. Write the code once and call it many times, which makes the program shorter.
- Easier to read. A meaningful name like
calculate_tax()says what the code does. - Easier to test and debug. Each subroutine can be tested on its own.
- Easier to maintain. A change or bug fix is made in one place, and every call benefits.
- Divide up a large program. Different programmers can write different subroutines.
- Reusable. The same subroutine can be used in other programs.
Worked example
Task: a program shows the area of three rooms. Without subroutines, the area calculation and display are repeated.
# Repeated code
print("Kitchen:", 4 * 3, "m2")
print("Lounge:", 6 * 5, "m2")
print("Bedroom:", 4 * 4, "m2")
With a subroutine:
def show_area(name, length, width):
area = length * width
print(name + ":", area, "m2")
show_area("Kitchen", 4, 3)
show_area("Lounge", 6, 5)
show_area("Bedroom", 4, 4)
If the output format needed to change, for example to show square metres as sq m, only one line inside show_area would need editing. In the first version, you'd have to change every line.
Exam tip: "advantages of subroutines" is a common question. Pick specific points and say why they matter. "Code can be reused, so it only needs to be written and tested once" scores better than "it is shorter".
Check yourself
What is the difference between a procedure and a function?
A function returns a value to the code that called it. A procedure carries out a task without returning a value.
What happens when a subroutine finishes?
Control returns to the statement just after the one that called it.
Give two advantages of using subroutines
Any two of: avoids repeating code; makes the program easier to read; each part can be tested separately; changes only need to be made in one place; a large program can be shared between programmers; subroutines can be reused in other programs.