How do subroutines, parameters and scope let us structure and reuse code?
Understand and use subroutines (procedures and functions), parameters, return values, local and global variables, scope, and the use of an interface and recursion.
A focused answer to AQA A-Level Computer Science 4.1.4 and 4.1.6, covering procedures and functions, parameters and return values, local and global scope, the benefits of subroutines, and recursion.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
AQA wants you to distinguish a procedure from a function, pass data with parameters and return results, explain local and global variables and scope, give the advantages of subroutines, and describe recursion and how it uses the call stack.
Procedures and functions
FUNCTION area(width, height) # parameters
RETURN width * height # return value
ENDFUNCTION
a = area(4, 5) # call, a = 20
The practical difference is how you use them: a function is called where a value is expected (on the right of an assignment or inside a larger expression), whereas a procedure is called as a statement in its own right to make something happen, such as drawing to the screen. Both let a single task be defined once and invoked wherever needed.
Parameters, return values and scope
Parameters are the named inputs in the subroutine definition; arguments are the actual values passed in when it is called. A function passes a result back with a return value. Data can be passed by value (the subroutine receives a copy, so changes do not affect the original) or by reference (the subroutine can change the caller's data), a distinction worth knowing for explaining side effects.
Advantages of subroutines and the interface
Subroutines support modular (structured) programming: a large problem is decomposed into smaller, named tasks. The benefits are reuse (write once, call many times), easier testing and debugging (test each subroutine independently), readability (a well-named subroutine documents its purpose), and the ability for several people to work on different subroutines in parallel. The interface of a subroutine is its name, parameters and return type: the information a caller needs in order to use it without seeing the implementation. This separation between interface and implementation is a form of abstraction, and it is what lets one part of a program change internally without breaking the parts that call it.
Recursion
FUNCTION factorial(n)
IF n = 0 THEN
RETURN 1 # base case
ELSE
RETURN n * factorial(n - 1) # general case
ENDIF
ENDFUNCTION
Recursion and iteration can often solve the same problem. Recursion can express naturally recursive structures (trees, divide-and-conquer algorithms, traversals) very concisely, but it uses stack memory proportional to its depth and carries the overhead of repeated calls. Iteration usually uses constant extra memory but can be more awkward for inherently recursive problems. Being able to weigh this trade-off, conciseness versus memory use, is what higher-mark questions on recursion reward.
Exam-style practice questions
Practice questions written in the style of AQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AQA 20194 marksExplain the difference between a local variable and a global variable, and give two reasons why using local variables is generally preferred when writing subroutines.Show worked answer →
A local variable is declared inside a subroutine and exists only while that subroutine is running; it cannot be seen or changed from outside. A global variable is declared in the main program and is visible throughout the whole program. The region of code over which a variable can be seen is its scope.
Local variables are preferred for two main reasons. First, they prevent accidental interference: because they are invisible elsewhere, one subroutine cannot accidentally change another's data, avoiding side effects. Second, they allow the same name to be reused safely in different subroutines without clashing, and they free their memory when the subroutine ends, which also makes each subroutine self-contained and easier to test.
Markers reward the scope-based definition and two distinct valid reasons (no side effects, name reuse, self-contained or testable, memory freed).
AQA 20225 marksA recursive function calculates the factorial of a number. Explain how recursion works in this function, state what would happen if the base case were omitted, and trace the calls made to evaluate factorial(3).Show worked answer →
Recursion is a function calling itself with a smaller version of the problem until it reaches a base case that returns a value directly without further recursion. For factorial, the base case is factorial(0) = 1, and the general case is factorial(n) = n times factorial(n - 1), each call reducing n towards 0.
If the base case were omitted, the function would call itself with ever-decreasing values that never stop, so each call would push a frame onto the call stack until the stack runs out of memory, causing a stack overflow.
Tracing factorial(3): factorial(3) calls factorial(2) calls factorial(1) calls factorial(0), which returns 1. Then factorial(1) returns , factorial(2) returns , and factorial(3) returns .
Markers reward explaining base and general cases, the stack overflow consequence, and the correct trace ending in 6.
Related dot points
- Understand and use the three basic programming constructs (sequence, selection and iteration), definite and indefinite iteration, nested constructs, and the meaning of constants and variables.
A focused answer to AQA A-Level Computer Science 4.1.2, covering the three basic constructs sequence, selection and iteration, definite and indefinite iteration, nesting, and the difference between constants and variables.
- Understand classes and objects, attributes and methods, instantiation, encapsulation, inheritance and polymorphism, and the principle of object-oriented design.
A focused answer to AQA A-Level Computer Science 4.1.5, covering classes and objects, attributes and methods, instantiation, encapsulation, inheritance and polymorphism, and the benefits of object-oriented design.
- Understand the built-in data types: integer, real or float, Boolean, character and string, and understand records, arrays and user-defined data types built from them.
A focused answer to AQA A-Level Computer Science 4.1.1, covering the built-in data types (integer, real, Boolean, character, string), how each is stored, and how records, arrays and user-defined types are built from them.
- Understand the stack abstract data type, LIFO behaviour, the push, pop and peek operations using a stack pointer, and the use of stacks for subroutine calls and recursion.
A focused answer to AQA A-Level Computer Science 4.2.3, covering the stack abstract data type, LIFO behaviour, the push, pop and peek operations with a stack pointer, overflow and underflow, and the use of the call stack.
- Understand depth-first and breadth-first graph traversal, the data structures they use, and the pre-order, in-order and post-order tree traversal algorithms.
A focused answer to AQA A-Level Computer Science 4.3.1, covering depth-first and breadth-first graph traversal and the data structures they use, plus the pre-order, in-order and post-order tree traversal algorithms.
Sources & how we know this
- AQA A-level Computer Science (7517) specification — AQA (2015)