Subroutines and Recursion
Aligned to the AQA 7517 specification
- Level
- Intermediate
- Reading time
- 6 min
- Published
- 13 June 2026
- Updated
- 1 July 2026
On this page
Key takeaways
- A subroutine is a named reusable block of code; a procedure returns no value while a function always executes a RETURN statement and returns one.
- Formal parameters are the names in the definition; actual parameters are the values passed at the call. Pass by value copies the value, while pass by reference passes the address so changes affect the original.
- Local variables exist only while their subroutine runs and cannot be accessed outside it; global variables are declared outside all subroutines and are accessible anywhere.
- Each subroutine call creates a stack frame on the call stack holding the return address, local variables and parameter values; the frame is popped when the call returns.
- Every recursive subroutine needs a base case that returns without recursing and a recursive case that calls itself on a smaller input; without a base case the call stack overflows.
Worth saving these ideas?
Turn what you've read into instant revision cards. Free to get started.
Key terms
- Subroutine
- A named, reusable block of code that performs a specific task, reducing duplication and making programs easier to read, test and maintain.
- Procedure
- A subroutine that performs effects such as output or modifying data but returns no value, defined with the AQA keyword PROCEDURE.
- Function
- A subroutine that returns a value and must always execute a RETURN statement before it ends.
- Formal parameter
- A name listed in the subroutine definition that receives an argument when the subroutine is called.
- Actual parameter
- An argument: the value passed to a subroutine when it is called.
- Pass by value
- A parameter-passing mechanism where a copy of the value is passed, so changes inside the subroutine do not affect the original variable.
- Pass by reference
- A parameter-passing mechanism where the variable's address is passed, so changes inside the subroutine modify the original variable.
- Local variable
- A variable declared inside a subroutine that exists only while that subroutine runs and cannot be accessed from outside it.
- Global variable
- A variable declared outside all subroutines that is accessible from anywhere in the program.
- Stack frame
- A record created on the call stack for each subroutine call, storing the return address, local variables and parameter values.
- Call stack
- The stack of frames built up by active subroutine calls; a frame is popped when its call returns, and deep recursion can exhaust it.
- Base case
- The condition in a recursive subroutine under which it returns without calling itself, allowing the recursion to terminate.
Frequently asked questions
A function returns a value and must always execute a RETURN statement before it ends, using the AQA keywords FUNCTION / RETURN. A procedure (keyword PROCEDURE) produces effects such as output or modifying data but returns nothing.
With pass by value a copy of the value is passed, so changing the parameter inside the subroutine does not affect the original variable. With pass by reference the variable's address is passed, so changes inside the subroutine do modify the original.
The base case is a condition under which the subroutine returns without calling itself, which is what makes the recursion terminate. Without it the subroutine calls itself indefinitely, building stack frames until the call stack overflows.
Generate revision on any topic you study
Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.
Lessons on anything
Structured, level-matched lessons on any topic you study
Practice quizzes
Find out what you actually know before the exam does
Flashcard sets
Lock in key concepts with instant revision cards
Ask Aica
Stuck on something? Get a clear explanation, any time
Programming Fundamentals
Object-Oriented Programming
Related lessons
7 min
9 min