Defining Procedures · Calling · Benefits of Subroutines
CSZoneAQA GCSE Computer Science 8525
What is a Subroutine?
Named Blocks of Code
A subroutine is a named block of code that performs a specific task. It can be called (executed) from anywhere in the program. AQA has two types: procedures (no return value) and functions (return a value).
PROCEDURE
Performs a task but does NOT return a value
FUNCTION
Performs a task AND returns a value back to the caller
Defining a Procedure in AQA
SUBROUTINE … ENDSUBROUTINE
DEFINE
SUBROUTINE greet() OUTPUT 'Hello!' OUTPUT 'Welcome to CSZone.' ENDSUBROUTINE
CALL (invoke)
greet() ← Hello! ← Welcome to CSZone.
greet() ← call again ← Hello! ← Welcome to CSZone.
Key:The subroutine is defined once but can be called many times — write once, reuse everywhere.
Benefits of Subroutines
Why Use Subroutines?
Reusability — define once, call many times; no need to repeat code
Readability — main program is shorter and easier to understand
Maintainability — fix a bug in one place; change takes effect everywhere
Decomposition — break large problems into smaller, manageable tasks
Real-world analogy:A subroutine is like a recipe — write it once and follow it whenever you need it.
Exam Practice
Have a go at this question
AQA-style question
Write a procedure called printLine that outputs 20 asterisk (*) characters in a row. Then call the procedure twice from the main program.
4 marks
SUBROUTINE printLine() line ← '' FOR i ← 1 TO 20 line ← line + '*' ENDFOR OUTPUT line ENDSUBROUTINE