GCSE Computer Science

Python subroutines โ€” procedures and functions โ€” GCSE Computer Science

What is a subroutine?

A subroutine is a named, reusable block of code โ€” Python's general term for what functions and procedures both are. Subroutines exist for three practical reasons: decomposition (breaking a big problem into smaller, named pieces), reuse (writing logic once and calling it many times), and testing (a subroutine with one clear job is far easier to test in isolation than a line buried inside a hundred-line program).

Procedure vs function

GCSE specifications draw a distinction that Python itself doesn't enforce in its syntax: a procedure performs an action but does not return a value; a function performs an action and returns a value back to the code that called it. In Python, both are written with def โ€” the only difference is whether the body contains a return statement that sends a value back.

def greet(name):
    print("Hello, " + name)

greet("Alice")

This is a procedure. It does something โ€” prints a message โ€” but gives nothing back. Call it, and there's no value to store or reuse afterwards.

def add(a, b):
    return a + b

result = add(3, 4)
print(result)

This is a function. return a + b sends the result of the calculation back to wherever add(3, 4) was called from โ€” result stores that value, ready to be used again elsewhere in the program.

Parameters and arguments

A parameter is the named variable a subroutine expects. An argument is the actual value passed in when it's called. In def add(a, b), a and b are parameters. In add(3, 4), 3 and 4 are the arguments. The subroutine runs the same logic every time โ€” only the arguments change what it does with that logic.

Local vs global scope

A variable created inside a subroutine is local โ€” it only exists while that subroutine is running, and no code outside it can see it. A variable created outside any subroutine is global โ€” visible everywhere in the program, including inside subroutines.

count = 0

def increment():
    local_value = 10
    print(local_value)

increment()
print(count)   # 0 - unaffected by what happened inside increment()

local_value only exists inside increment() โ€” trying to print(local_value) outside the function would raise an error, because outside that scope, it was never created.

Common exam mistake

Writing print inside a subroutine and calling this "returning a value." The subroutine produces output but returns None. If the calling code tries to use the return value, it gets None. Examiners test this distinction directly.

What examiners actually test

Subroutine questions usually ask you to identify whether a given piece of code is a procedure or a function, trace what value (if any) gets returned, or write a subroutine to a specification that explicitly asks for a return value. The line worth memorising precisely: no return statement โ€” or a bare return with nothing after it โ€” means it's a procedure, even if it prints something to the screen.

The procedure-vs-function line looks simple until you're marking twenty different attempts at the same task. Python Coach standardises the practice โ€” 195 challenges across 27 lessons, with progress tracking so you can see exactly who's still printing instead of returning. Sixty teaching days free, no payment details required.

Start your school's free trial →