A subroutine is a named block of code designed to perform a specific task that can be called from anywhere in a program. Subroutines avoid repeating code and make programs easier to read, test, and maintain.
There are two types of subroutine in Edexcel 1CP2:
| Type | Returns a value? | Edexcel pseudocode | Example |
|---|---|---|---|
| Function | Yes — returns a single value | FUNCTION … RETURN … ENDFUNCTION | calculateArea(r) |
| Procedure | No — performs actions without returning a value | PROCEDURE … ENDPROCEDURE | printGreeting(name) |
FUNCTION square(n : INTEGER) : INTEGER
RETURN n * n
ENDFUNCTION
result ← square(5) // argument = 5, returns 25
| Type | Scope | Description |
|---|---|---|
| Local variable | Inside the subroutine only | Created when the subroutine is called, destroyed when it ends. Cannot be accessed outside. Prevents unintended side effects. |
| Global variable | Whole program | Declared outside all subroutines. Can be read/modified from anywhere. Can cause bugs if accidentally changed. |
Procedure:
PROCEDURE greetUser(name : STRING)
OUTPUT "Hello, " + name
ENDPROCEDURE
CALL greetUser("Alice") // outputs: Hello, Alice
Function:
FUNCTION circleArea(r : REAL) : REAL
RETURN 3.14159 * r * r
ENDFUNCTION
area ← circleArea(5) // returns 78.54
8 questions · Functions & Procedures
calculateGrade(score) is better than repeating the grade calculation logic 200 times.[3]maximum that takes two integers a and b and returns the larger value.[3]| Term | Definition |
|---|
10 minutes · Exam-style