You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
A subroutine is a named block of code that performs a specific task. Subroutines make programs easier to read, test, debug, and maintain. They are a crucial part of GCSE Computer Science (AQA 3.2 / OCR J277 2.2).
A subroutine is a self-contained section of code that can be called (invoked) by name whenever it is needed. Instead of writing the same code multiple times, you write it once as a subroutine and call it whenever required.
There are two types of subroutine:
| Type | Description | Returns a value? |
|---|---|---|
| Procedure | Performs a task but does NOT return a value | No |
| Function | Performs a task and RETURNS a value | Yes |
Exam Tip: The key distinction is that a function returns a value that can be stored in a variable or used in an expression. A procedure simply carries out an action.
A procedure performs an action without returning a value.
SUBROUTINE greet(name)
OUTPUT "Hello, " + name + "!"
ENDSUBROUTINE
greet("Alice")
greet("Bob")
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.