Subroutines — Procedures and Functions

intermediate25 min

Learning objectives

  • Distinguish between procedures and functions
  • Explain the benefits of modular programming
  • Decompose problems using top-down design
  • Develop reusable subroutines in Python

Learn

AQA 4.1.1.10 — Subroutines, modularity and top-down design

A subroutine is a named, reusable block of code. AQA distinguishes:

  • Procedures: perform an action, don't return a value.
  • Functions: perform an action and return a value.

In Python both are written with def; a function uses return to send a value back.

def greet(name):          # procedure-like: no return value
    print(f"Hello, {name}!")

def square(n):             # function: returns a value
    return n * n

greet("Aisha")
result = square(5)         # result = 25

Why modularise?

  • Code reuse — write the logic once, call it many times.
  • Top-down design — break a big problem into smaller, named sub-problems you can design and test independently.
  • Readability — a well-named function documents intent without a comment.

Top-down design in practice

A menu-driven calculator decomposes naturally:

Calculator
├── show_menu()
├── get_numbers()
├── add(a, b)
├── subtract(a, b)
├── multiply(a, b)
└── divide(a, b)
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def show_menu():
    print("1. Add\n2. Subtract")

def get_numbers():
    a = float(input("First number: "))
    b = float(input("Second number: "))
    return a, b

show_menu()
choice = input("Choose an option: ")
x, y = get_numbers()
if choice == "1":
    print(add(x, y))
elif choice == "2":
    print(subtract(x, y))

Challenge

Refactor a program you wrote earlier in Sequence 1 or 2 into at least two separate, sensibly named functions.

Practise

Apply what you've just learned in the Coding Lab.

Open Coding Lab

Test yourself

Check your understanding with exam-style questions.

Go to Exam Practice
Log in to track this lesson on your progress dashboard.
Log in