Subroutines, Parameters & Variable Scope (OCR A-Level CS 2.2.1)

OCR A-Level CS 2.2.1: functions vs procedures, passing parameters by value and by reference, and the difference between local and global variable scope.


Free Subroutines Functions Scope revision resources (OCR A-Level Computer Science, 2.2.1)

We’ve made exam-style practice for this exact topic, free to download: Subroutines Functions Scope question sheet, mark scheme and cheat sheet. Grab them, have a go, then read the full guide below.

Real programs are built from subroutines — named, reusable blocks of code. Spec points 2.2.1(c) and (d) cover modularity: functions vs procedures, passing parameters by value and by reference, and the difference between local and global variables. Paper 2 tests this with "complete the function" tasks and a regular 9-mark essay comparing local variables and parameters with globals — so it's well worth nailing.

Let's build it up.

Subroutines: functions vs procedures

A subroutine is a named block of code you can call by name. Breaking a program into subroutines is modularity — and it makes code easier to write, test, reuse and maintain. OCR distinguishes two kinds:

  • A function returns a value to wherever it was called. total = add(3, 5)add hands back a result.

  • A procedure carries out a task but doesn't (necessarily) return a value. printReceipt(order) just does something.

The rule of thumb: if you need an answer back, write a function; if you just need a job done, a procedure is fine.

Parameters and arguments

A subroutine often needs data to work on, supplied through parameters:

  • Parameters are the named placeholders in the subroutine's definitionfunction add(a, b).

  • Arguments are the actual values you pass in when you call it — add(3, 5) passes 3 and 5.

Passing data in through parameters (rather than relying on global variables) keeps each subroutine self-contained — it works on exactly what it's given.

Passing by value vs by reference

There are two ways a parameter can be passed:

  • By value — a copy of the data is passed in. The subroutine can change its copy, but the original variable outside is unaffected. Safer, and the default to assume unless told otherwise.

  • By reference — a reference (the memory location) of the original is passed. The subroutine works on the actual variable, so any change it makes persists after the subroutine ends.

Quick test: pass x by value into a routine that doubles it, and x is unchanged outside; pass it by reference, and x really is doubled. Exam questions love asking which one a given scenario needs.

Local vs global variables

A variable's scope is where in the program it can be accessed:

  • A local variable is declared inside a subroutine and only exists while that subroutine runs. Nothing outside can see it, and it's destroyed when the subroutine ends.

  • A global variable is declared in the main program and can be accessed anywhere, including inside subroutines.

Why prefer local variables and parameters? This is the 9-mark comparison:

  • No accidental interference — a local variable can't be changed by unrelated code, so bugs are rarer and easier to find.

  • Reusability and modularity — a subroutine that uses only locals and parameters is self-contained and can be reused or moved without breaking other code.

  • No naming clashes — two subroutines can both use a variable called count without affecting each other.

  • Memory — locals are freed when the subroutine ends.

Globals aren't always wrong — a value genuinely needed everywhere (like a running game score) can justify one — but overusing globals makes large programs fragile and hard to debug.

Worked example: by value vs by reference




If n were instead passed by reference, the same code would print 10, because tryDouble would be working on the real x, not a copy. Same code, different parameter-passing — different result. That contrast is exactly what the exam wants you to explain.

Common exam mistakes

  • Function vs procedure. A function returns a value; a procedure performs a task. Don't use them interchangeably.

  • Parameter vs argument. Parameters are in the definition; arguments are the values passed at the call.

  • By value vs by reference. By value = a copy (original safe); by reference = the original (changes persist).

  • Claiming locals are visible outside. A local variable only exists inside its subroutine and is gone afterwards.

  • One-sided scope essays. "Compare global and local" wants the benefits of locals/parameters and a fair note on when a global is justified.

Quick recap

  • A subroutine is a named, callable block — modularity makes code easier to write, test, reuse and maintain.

  • Function = returns a value; procedure = performs a task.

  • Parameters (in the definition) receive arguments (the values passed at the call).

  • By value = a copy is passed (original unchanged); by reference = the original is passed (changes persist).

  • Local variables exist only inside their subroutine; global variables are accessible everywhere — prefer locals + parameters to avoid bugs and keep code modular.

Frequently asked questions

What is the difference between a function and a procedure? A function is a subroutine that returns a value to the place it was called from, such as add(3, 5) returning 8. A procedure is a subroutine that carries out a task but does not necessarily return a value, such as printing a receipt. Both are subroutines; the difference is whether a value is returned.

What is the difference between a parameter and an argument? A parameter is the named placeholder in a subroutine's definition, such as a and b in function add(a, b). An argument is the actual value passed in when the subroutine is called, such as the 3 and 5 in add(3, 5). Parameters belong to the definition; arguments belong to the call.

What is the difference between passing by value and passing by reference? Passing by value sends a copy of the data, so changes made inside the subroutine do not affect the original variable. Passing by reference sends the memory location of the original, so changes made inside the subroutine do persist after it ends. By value is safer; by reference lets a subroutine modify the caller's variable.

What is the difference between a local and a global variable? A local variable is declared inside a subroutine and can only be accessed there, and it is destroyed when the subroutine ends. A global variable is declared in the main program and can be accessed anywhere, including inside subroutines. Scope is the part of the program where a variable can be used.

Why are local variables and parameters preferred over global variables? Local variables and parameters keep subroutines self-contained, so unrelated code cannot accidentally change them, which makes bugs rarer and easier to find. They also avoid naming clashes, support reuse and modularity, and free memory when the subroutine ends. Overusing global variables makes large programs fragile and hard to debug.

What is modularity? Modularity is the practice of breaking a program into separate subroutines, each performing a specific task. It makes a program easier to write, test, debug, reuse and maintain, because each module can be developed and checked independently and shared between programs.

 

 

 

Logo

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, trademarks and brands does not imply endorsement.


Follow us on:

Icon
Icon
Icon
Icon
Icon

Support@shuttlelearning.com

Logo

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, trademarks and brands does not imply endorsement.


Follow us on:

Icon
Icon
Icon
Icon
Icon

Support@shuttlelearning.com

Logo

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, trademarks and brands does not imply endorsement.


Follow us on:

Icon
Icon
Icon
Icon
Icon

Support@shuttlelearning.com