Procedures, Functions and Variable Scope
1. Why Use Subroutines?
A subroutine is a named block of code that performs one specific task and can be called from anywhere in a program. Cambridge pseudocode has two kinds: procedures and functions.
Subroutines are used because they:
- avoid repeating the same code in several places
- make a program easier to read, test and maintain
- allow one task to be written and tested independently of the rest of the program
- let a large problem be broken down (decomposed) into smaller parts
2. Procedures vs Functions
This distinction is examined regularly, and the difference is exactly one thing: whether a value is returned.
| Procedure | Function | |
|---|---|---|
| Returns a value? | No | Yes — always returns exactly one value |
| Keywords | PROCEDURE … ENDPROCEDURE | FUNCTION … RETURNS … ENDFUNCTION |
| How it is called | CALL Name(…) | Used inside an expression, e.g. x ← Name(…) |
| Typical use | Perform an action, e.g. display a menu | Calculate and hand back a result, e.g. an area |
A procedure
A function
3. Parameters
A parameter is a value passed into a subroutine so that the same subroutine can work on different data each time it is called.
| Term | Meaning |
|---|---|
| Parameter | The variable named in the subroutine definition, e.g. Width |
| Argument | The actual value supplied when the subroutine is called, e.g. 5 |
A subroutine may take no parameters at all (as with DisplayMenu()), or several.
4. Local and Global Variables
Where a variable is declared decides which parts of the program can see and use it. This is called the variable's scope.
| Local variable | Global variable | |
|---|---|---|
| Declared | Inside a subroutine | Outside every subroutine, in the main program |
| Can be used by | Only that subroutine | The whole program, including all subroutines |
| Exists | Only while the subroutine is running | For as long as the program runs |
Why local variables are preferred
- They cannot be changed accidentally by another part of the program.
- The same variable name can be reused safely in different subroutines.
- They free up memory when the subroutine finishes.
- A subroutine using only local variables and parameters can be tested on its own.
5. Nested Statements
A statement is nested when it is placed inside another statement of the same kind — a loop inside a loop, or an IF inside an IF.
The inner loop completes in full for every single pass of the outer loop. In the grid example the outer loop runs 3 times and the inner loop runs 4 times per pass, so OUTPUT happens 3 × 4 = 12 times.
6. Exam Focus
Quick self-check
- State the one difference between a procedure and a function.
- Write a function that takes two parameters and returns the larger of them.
- Explain why Bonus in the example above cannot be output from the main program.
- State the maximum number of parameters a subroutine may have in this syllabus.
- How many times does OUTPUT run if the outer loop is 1 to 5 and the inner is 1 to 3?