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.
3b. Passing Parameters: By Value and By Reference
When an argument is passed to a subroutine there are two ways it can travel, and the syllabus names both. The difference is whether the subroutine can change the caller's variable.
- By value — a copy of the data is passed. The subroutine works on the copy, so any change it makes is discarded when the subroutine ends. The original variable is untouched.
- By reference — the location of the original variable is passed. The subroutine works on the original itself, so any change it makes persists after the subroutine ends.
| By value | By reference | |
|---|---|---|
| What is passed | A copy of the value | The address of the original |
| Original changed? | No | Yes |
| Use it when | The subroutine only needs to read the value | The subroutine must update the caller's variable |
Score holds 10 and is passed to a
subroutine that doubles its parameter. Passed by value, the subroutine's copy
becomes 20, and after the call Score is still 10. Passed
by reference, the subroutine changes the original, and after the call
Score is 20. Same subroutine, same argument, different result
— and the only difference is how the parameter was passed.
Why by value is the safer default: a subroutine that cannot alter the caller's variables cannot cause an unexpected change somewhere else in the program. Passing by reference is more efficient for large data such as an array, since nothing is copied, but it gives the subroutine the power to modify data its caller may not expect to change.
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?