Subroutines: Procedures and Functions
Understand Subroutines: Procedures and Functions for GCSE Computer Science with this free worksheet and full mark scheme — Foundation and Higher exam-style questions with worked answers for AQA GCSE Computer Science (8525). Subroutines are named blocks of code; a function returns a value while a procedure does not.
Free downloads
These worksheets and mark schemes are original, written for Virtus Academy and checked against the current AQA specification. Every worksheet comes with a full mark scheme.
Topic overview
A subroutine is a named block of code that performs a specific task and can be called from elsewhere in the program.
There are two kinds. A procedure carries out a task but does not return a value. A function carries out a task and returns a value to the point where it was called, so it can be used within an expression.
Subroutines are the practical expression of decomposition. They avoid repeating code, since a subroutine written once can be called many times. They make programs easier to test, because each can be checked independently. And they make code readable, since a well-named call describes what happens without the reader needing the detail.
Revision notes
Procedures and functions
A procedure performs a task but returns no value.
A function performs a task and returns a value, so it can be used inside an expression: total ← add(3, 4). This is the defining difference and is examined directly.
Why use subroutines
They avoid repeated code, since one subroutine can be called many times.
They make testing easier, because each can be tested independently. They make programs readable, since a well-named call describes the action without needing the detail.
Link to decomposition
Subroutines are how decomposition appears in actual code.
Each sub-problem identified during decomposition becomes one subroutine with a clearly defined job. This is why the two topics are frequently examined together.
Key points
- A subroutine is a named block of code.
- A procedure returns no value.
- A function returns a value.
- A function call can be used in an expression.
- Subroutines avoid repeating code.
- Each sub-problem becomes one subroutine.
Worked examples
Example 1
State the difference between a procedure and a function. [2 marks]
Working
Example 2
Give two benefits of using subroutines. [2 marks]
Working
Example 3
Explain how subroutines relate to decomposition. [2 marks]
Working
Common mistakes
Saying a procedure returns a value.
Only a function does.
Using a procedure call inside an expression.
It returns nothing, so there is no value to use.
Giving only one benefit.
Reuse, testability and readability are all examinable.
Confusing calling with defining.
Defining creates the subroutine; calling runs it.
Exam tips
- Learn the procedure and function difference precisely.
- Give several benefits when asked.
- Link subroutines to decomposition.
- Use meaningful subroutine names.
Key terms
- Subroutine
- A named block of code performing a specific task.
- Procedure
- A subroutine that returns no value.
- Function
- A subroutine that returns a value.
- Call
- To run a subroutine from elsewhere in the program.
Related topics
Written and reviewed against the current AQA specification. Spotted an error? Let us know.