How do we split a program into reusable named blocks?
Use subroutines (procedures and functions), pass parameters and return values, and understand the scope of local and global variables.
A focused answer to AQA GCSE Computer Science 3.2.8, covering subroutines (procedures and functions), passing parameters, returning values, and the scope of local and global variables.
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 use subroutines (procedures and functions), pass parameters into them and return values from functions, and explain the scope of local and global variables.
Subroutines
Procedures and functions
FUNCTION area(radius)
RETURN 3.14159 * radius * radius
ENDFUNCTION
Here area(5) returns a value you can store or use, whereas a procedure such as printMenu() simply does something (displays a menu) and returns nothing.
Parameters
Parameters are what make a subroutine general: area(radius) works for any radius because the value is passed in, rather than the radius being fixed inside the subroutine.
Variable scope
Local variables are usually preferred because they cannot be changed accidentally by other parts of the program, which avoids side effects and makes subroutines self-contained, easier to test and safe to reuse. A local variable is also created fresh each time its subroutine runs and destroyed when it ends, so two subroutines can use the same name without clashing.
Why subroutines are worth using
Subroutines bring the same benefits as structured programming because they are its building block. Writing a task once as a subroutine and calling it many times avoids repeating code, so there is less to write and only one place to fix if it is wrong. Each subroutine can be tested on its own with known inputs and expected outputs before the rest of the program is finished. A clear interface (parameters in, return value out) plus local variables keeps a subroutine self-contained, so it can be reused in other programs and changed internally without affecting the code that calls it, as long as the interface stays the same.
Try this
Q1. State one difference between a function and a procedure. [2 marks]
- Cue. A function returns a value; a procedure carries out a task without returning one.
Q2. Give one reason local variables are preferred to global variables. [1 mark]
- Cue. They cannot be changed accidentally by other subroutines, avoiding side effects.
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 20205 marksWrite an AQA-style function called area that takes the radius of a circle as a parameter and returns its area, using 3.14159 for pi. State what value area(2) returns and explain why a function rather than a procedure is appropriate here.Show worked answer →
A correct function:
FUNCTION area(radius) RETURN 3.14159 * radius * radiusENDFUNCTION
area(2) returns .
A function is appropriate because the subroutine produces a value (the area) that is needed back at the point of call, and a function returns a value whereas a procedure does not.
Markers reward a function with a parameter and a RETURN of the correct formula, the value about 12.57, and the reason (it returns a value).
AQA 20224 marksExplain the difference between a local and a global variable, and explain one reason why using local variables in subroutines is considered good practice.Show worked answer →
A local variable is declared inside a subroutine and only exists, and can only be used, within that subroutine; it is created when the subroutine runs and destroyed when it ends. A global variable is declared in the main program and can be used anywhere in the program.
Using local variables is good practice because they cannot be changed accidentally by other parts of the program, which avoids unexpected side effects and keeps each subroutine self-contained and easier to test and reuse.
Markers reward the scope distinction (inside one subroutine versus whole program) and a reason (avoids accidental change and side effects).
Related dot points
- Use the three programming constructs of sequence, selection and iteration, including definite and indefinite iteration, and nest them.
A focused answer to AQA GCSE Computer Science 3.2.2, covering the three programming constructs of sequence, selection and iteration, the difference between definite and indefinite iteration, and nesting.
- Apply the principles of structured programming, breaking a problem into subroutines with clear interfaces, and explain the benefits of this approach.
A focused answer to AQA GCSE Computer Science 3.2.11, covering the principles of structured programming, breaking a problem into subroutines with clear interfaces, and the benefits of the approach.
- Use the common data types, declare and assign variables and constants, and understand the difference between a variable and a constant.
A focused answer to AQA GCSE Computer Science 3.2.1, covering the common data types, declaring and assigning variables and constants, and the difference between a variable and a constant.
- Use common string-handling operations including length, position, substring, concatenation, and converting between case and between strings and numbers.
A focused answer to AQA GCSE Computer Science 3.2.9, covering common string-handling operations such as length, position, substring, concatenation, case conversion and converting between strings and numbers.
Sources & how we know this
- AQA GCSE Computer Science (8525) specification — AQA (2020)