Subroutines — procedures and functions
A subroutine is a named block of code you can call from elsewhere in your program. AQA distinguishes two kinds:
- Procedure — performs a task; doesn't return a value.
- Function — performs a calculation and returns a value to the caller.
Both can take parameters — values passed in by the caller.
Why use subroutines?
- Decomposition — a big problem becomes several smaller ones.
- Reuse — write once, call from many places.
- Readability —
calculateTotal(items)reads better than 12 lines of inline code. - Testability — small subroutines are easier to test in isolation.
- Maintenance — fix a bug in one place, not twelve.
Procedure syntax
PROCEDURE greet(name)
OUTPUT "Hello, " + name
ENDPROCEDURE
-- call site:
greet("Aisha")
Function syntax
FUNCTION square(n)
RETURN n * n
ENDFUNCTION
-- call site:
SET result TO square(5) -- 25
OUTPUT result
The RETURN statement immediately exits the function and hands the value back to whoever called it.
Parameters and arguments
- Parameter — the variable name in the subroutine definition (
nabove). - Argument — the actual value passed at the call site (
5above).
You can have several parameters, separated by commas:
FUNCTION rectangleArea(width, height)
RETURN width * height
ENDFUNCTION
OUTPUT rectangleArea(4, 6) -- 24
Local vs global variables
A local variable is declared inside a subroutine and only exists for the duration of that call. A global variable is declared outside any subroutine and is visible everywhere.
SET total TO 0 -- global
PROCEDURE addOne()
SET total TO total + 1
ENDPROCEDURE
PROCEDURE counter()
SET local TO 0 -- local
SET local TO local + 1
OUTPUT local
ENDPROCEDURE
Best practice: prefer local variables. Globals make programs harder to reason about because anything can change them. Pass values via parameters and return results.
✦Worked example— Worked example — refactor with subroutines
Without subroutines:
USERINPUT a
USERINPUT b
SET aSquared TO a * a
SET bSquared TO b * b
SET sum TO aSquared + bSquared
SET hyp TO SQRT(sum)
OUTPUT hyp
With a function:
FUNCTION hypotenuse(a, b)
RETURN SQRT(a * a + b * b)
ENDFUNCTION
USERINPUT a
USERINPUT b
OUTPUT hypotenuse(a, b)
The function can now be called from anywhere with any pair of values — that's reuse.
Returning multiple values
AQA pseudocode doesn't natively return multiple values. Workarounds:
- Return an array:
RETURN [min, max]. - Return a record (if available).
- Use multiple functions, each returning one value.
Recursion (extension)
A subroutine that calls itself. Not core to GCSE but recognise it:
FUNCTION factorial(n)
IF n <= 1 THEN
RETURN 1
ELSE
RETURN n * factorial(n - 1)
ENDIF
ENDFUNCTION
⚠Common mistakes— Pitfalls
- Forgetting to RETURN — function returns nothing/undefined.
- Forgetting parameters — calling
square()instead ofsquare(5). - Modifying globals from inside a subroutine — surprises the caller.
- Treating procedures as functions —
SET x TO greet("Aisha")won't work because greet is a procedure. - Variable name clashes — local
ninside the function shadows globaln.
➜Try this— Quick check — function or procedure?
For each task, decide:
- "Calculate the area of a circle from a radius." → Function (returns a value).
- "Print a banner of asterisks." → Procedure (no value to return).
- "Convert a string to uppercase." → Function (returns the new string).
- "Save data to a log file." → Procedure (side-effect, no return).
AI-generated · claude-opus-4-7 · v3-deep-computer-science