A subroutine is a named block of code that performs a specific task and can be called (invoked) from anywhere in a program. Parameters are the named inputs a subroutine accepts; return values are the outputs it sends back. Together they let you write reusable, well-structured code that is easier to test and maintain.

Why do we use subroutines at all?

Imagine a chef who writes out the same sauce recipe in full every time it appears in a cookbook — 47 times across 200 pages. If the recipe changes, every copy must be updated and any missed copy creates inconsistency. Programmers face the same problem. Subroutines solve it with the DRY principle (Don't Repeat Yourself): write the code once, name it, and call that name wherever you need it. Change the subroutine's body once and every call automatically uses the new behaviour.

What is the difference between a procedure and a function?

In GCSE terminology, both are subroutines, but they differ in one key way:

Type Returns a value? Typical use
Procedure No Performs an action (e.g. print a report, draw a shape)
Function Yes Computes and returns a result (e.g. calculate area, convert units)

Some languages (Python, JavaScript) use the same keyword (def / function) for both; the distinction lies in whether a return statement sends a value back to the caller. In AQA pseudocode, both use SUBROUTINE / ENDSUBROUTINE or FUNCTION / ENDFUNCTION.

What are parameters and arguments?

Parameters are the placeholder names listed in a subroutine's definition — they describe what the subroutine expects to receive.

Arguments are the actual values passed into the subroutine when it is called.

FUNCTION areaOfRectangle(width, height)    ← width, height are PARAMETERS
    RETURN width * height
ENDFUNCTION

result = areaOfRectangle(5, 3)             ← 5 and 3 are ARGUMENTS

The subroutine does not know (or care) where 5 and 3 come from. It treats them as width and height and calculates the area. This separation of interface (parameters) from caller (arguments) is a key design principle.

What is a return value and how is it used?

A return value is the result sent back to the calling code via the RETURN statement. The caller can store it in a variable, print it, pass it to another subroutine, or use it in an expression.

Worked example — temperature converter:

FUNCTION celsiusToFahrenheit(celsius)
    fahrenheit = (celsius * 9/5) + 32
    RETURN fahrenheit
ENDFUNCTION

temp_c = 100
temp_f = celsiusToFahrenheit(temp_c)
OUTPUT temp_f                         → outputs 212

Trace table:

Step Variable Value
Call with celsius = 100 celsius 100
fahrenheit = (100 × 9/5) + 32 fahrenheit 212
RETURN 212
temp_f receives 212 temp_f 212

What is the difference between local and global variables?

Local variables are declared inside a subroutine and exist only while that subroutine is running. Once the subroutine ends, local variables are destroyed and their values are lost.

Global variables are declared outside any subroutine and can be read by any part of the program.

Property Local Global
Where declared Inside a subroutine Outside all subroutines
Scope Only the subroutine it belongs to Entire program
Lifetime Created on call, destroyed on return Entire program run
Preferred? ✅ Yes — avoids side effects ⚠ Use sparingly

Why prefer local variables? A subroutine with only local variables is self-contained — it cannot accidentally change a value used elsewhere. This makes bugs far easier to find and fix. Global variables create hidden dependencies between unrelated parts of code.

How do you write a subroutine with multiple parameters?

FUNCTION gradeComment(score, maxScore)
    percentage = (score / maxScore) * 100
    IF percentage >= 70 THEN
        RETURN "Distinction"
    ELSE IF percentage >= 50 THEN
        RETURN "Merit"
    ELSE
        RETURN "Pass"
    END IF
ENDFUNCTION

OUTPUT gradeComment(75, 100)   → "Distinction"
OUTPUT gradeComment(53, 100)   → "Merit"
OUTPUT gradeComment(40, 100)   → "Pass"

The subroutine is called three times with different arguments each time — the same code handles all three cases without repetition.

Frequently asked questions

Can a subroutine return more than one value?

In standard pseudocode and many languages, a single return statement returns one value. To return multiple values, common approaches are: (1) return a list or array containing the values, (2) use a record/object, or (3) modify global variables (discouraged). Python directly supports return a, b which packs values into a tuple — the caller unpacks them with x, y = myFunction().

What is the difference between passing by value and passing by reference?

Pass by value: The subroutine receives a copy of the argument. Changes inside the subroutine do not affect the original variable. This is the default in most exam pseudocode and in Python for immutable types. Pass by reference: The subroutine receives a reference (address) to the original variable. Changes inside the subroutine directly modify the original. This is how Python handles mutable objects such as lists. At GCSE you are usually expected to know the distinction exists, not implement it from scratch.

Why do good programs avoid using global variables?

Global variables make code harder to understand because a subroutine can be affected by (or affect) values set anywhere else in the program — a property called a side effect. When debugging, you cannot simply read a subroutine in isolation; you must track every place the global could have changed. Local variables eliminate this problem by keeping each subroutine's data self-contained and predictable.

What happens if a function does not have a return statement?

Most languages implicitly return a null/None value if no explicit return is reached. In a procedure this is expected and harmless. In a function, a missing return is almost always a logic error — the caller receives None and any subsequent use of that value (e.g. arithmetic) causes a runtime error. Exam boards expect you to identify missing return statements as a bug.


Practise writing and tracing subroutines with Professor Turing's live feedback at aitutors.me.