Algorithms and programming · GCSE Computer Science
Subroutines and functions
GCSE Computer Science subroutines and functions: parameters, return values, local versus global variables, and why reuse beats copy-paste in exam code.
A function returns a value; a procedure (subroutine) does a job. Parameters pass data in. Local variables live inside; globals are visible everywhere and dangerous.
The important bits
What you need to know
- 1
Subroutines package a named block of code you can call more than once. They make programs shorter, easier to test, and easier to maintain — examiners call this modularity.
- 2
A function returns a value to the caller (a number, string, Boolean). A procedure / subroutine in some board languages does not return a value; it just runs. Use the paper’s words.
- 3
Parameters (arguments) pass data into the subroutine. The call calcArea(3, 7) supplies actual values; the definition FUNCTION calcArea(length, width) names the parameters.
- 4
Return sends a result back: RETURN length * width. After RETURN, that call is finished. Forgetting RETURN and printing inside the function instead can still “look right” and miss the mark.
- 5
Local variables exist only while the subroutine runs. Two functions can both have a variable called total without clashing. That is scope.
- 6
Global variables are declared outside subroutines and can be read or overwritten from anywhere. They are convenient in short traces and a source of unexpected side effects in longer programs.
- 7
Passing by value (typical GCSE story) means the subroutine gets a copy; changing the parameter does not change the caller’s variable unless you return a new value and assign it.
- 8
Call stack thinking: the caller pauses, the subroutine runs with its locals, it returns, the caller continues. Nested calls are allowed — a function can call another function.
Quotations worth analysing
Short evidence. Real method.
“A function returns a value; a procedure does not.”
If the paper uses “subroutine” for both, still say whether a value is returned. The return mark is separate from “it has a name”.
“Parameters allow data to be passed into a subroutine.”
Name parameters in the definition and arguments at the call. “It uses variables” is not enough — say they are passed in.
“A local variable can only be used within the subroutine in which it is declared.”
Contrast with global: available throughout the program. The drawback of globals is unintended changes from other subroutines.
Go deeper
Return is not the same as output
OUTPUT prints for a human. RETURN hands a value back to the caller so it can be stored, added, or tested. FUNCTION tax(price) that OUTPUTS 0.2 * price but does not RETURN it cannot be used as total ← net + tax(net). That distinction is a 2-mark regular. In a trace, when you hit RETURN x, write x in the caller’s assignment and leave the function; do not execute lines after RETURN. Parameters are filled from the call, in order, before the first line of the body. If the definition is FUNCTION max(a, b) and the call is max(score, 70), then a is score’s value and b is 70 for this call only. A second call with different arguments does not remember the first. Locals are created fresh each call — that is why recursion (if your spec includes it) does not smash the previous frame’s n.
Go deeper
Globals feel easier until they collide
A global total that every subroutine increments will work in a six-line program and then fail when two routines both assume they own it. Papers like a short advantage (any subroutine can use it, no need to pass it) and a short disadvantage (harder to debug, unexpected changes, less reusable). The better exam habit is: pass parameters in, return a result out, keep working storage local. If you must use a global in a trace because the printed program does, then every assignment to that name, in any subroutine, updates the same cell in the trace table — that is the whole point of the question. Do not invent a second total. Maintainability marks also live here: identifiers, one job per subroutine, and no copy-pasted blocks that drift apart when only one copy gets the bug fix.
See the idea in action
FUNCTION discount(price, rate) amount ← price * rate RETURN price − amount ENDFUNCTION pay ← discount(50, 0.1) OUTPUT pay Call: price = 50, rate = 0.1 (parameters). Local amount = 50 * 0.1 = 5. RETURN 50 − 5 = 45. The function ends; amount is discarded. Caller: pay = 45. OUTPUT 45. If discount used a global price instead of a parameter, another subroutine that set price ← 0 would silently break this call. If discount OUTPUT 45 but did not RETURN, then pay would not be 45 unless the caller also used that global. Parameters in, value out, locals gone after RETURN — that is the trace.
Exam technique
Turn knowledge into marks
On definitions, say parameter, return, local, global in plain mark-scheme English. In a trace, fill parameters from the call first, then locals, then the returned value in the caller.
Common mistakes
Do not give these marks away
- 01
Printing inside a function and forgetting to RETURN the value the caller needs to store.
- 02
Changing a parameter and expecting the original variable in the caller to change, without a RETURN and an assignment.
- 03
Using global variables for everything, then losing track of which subroutine last wrote to total.
What is the difference between a local variable and a global variable?
ALocal variables are stored on the hard drive; global variables are stored in cache
BA local variable exists only inside its subroutine; a global variable can be used throughout the program
CGlobal variables cannot store numbers
DLocal variables are always returned automatically
Show the answer
A local variable exists only inside its subroutine; a global variable can be used throughout the program. Scope is about where a name is visible. Locals vanish when the subroutine ends and do not clash with the same name elsewhere. Globals are shared, which is powerful and risky. Storage device and cache are irrelevant here.
Quick questions
If this is the bit you searched
What is a parameter in a subroutine GCSE?
A named input to the subroutine. The call supplies arguments; the definition receives them as parameters so the block can run with different values without rewriting the code.
What is the difference between a function and a procedure?
A function returns a value to the caller. A procedure (or a subroutine that does not return) performs actions such as output or updating a global. Use the wording your exam board prints.
Why use subroutines instead of repeating code?
Reuse, shorter programs, easier testing, and one place to fix a bug. That is maintainability. Examiners also like “can be called with different parameters”.
Can two subroutines use the same local variable name?
Yes. Each has its own local copy. They do not overwrite each other. Two globals with the same name are the same variable — that is the collision.