Why are local variables preferred to global ones?
- They exist only within their subroutine.
- Other code cannot change them by accident.
- Subroutines are easier to test and reuse.
Answer They avoid unintended side effects and make subroutines reusable
A-level Computer Science revision · Programming
Subroutines are named blocks of code. A function returns a value; a procedure does not. Parameters pass data in, by value (a copy) or by reference (the original). Local variables exist only inside their subroutine; global variables are accessible everywhere, but make code harder to test.
Watch out Changing a parameter passed by value inside a subroutine does not change the original variable.
Why are local variables preferred to global ones?
Answer They avoid unintended side effects and make subroutines reusable
What is the difference between a function and a procedure?
A parameter passed by reference is changed inside a subroutine. What happens to the original variable?
Give one advantage of using parameters instead of global variables.
Answer Parameters make clear what data a subroutine uses, so it can be reused and tested independently without unexpected changes elsewhere.
A-level Computer Science Pro, OCR H446
£119 once
Price rises to £199 on 1 January. Save £80 by joining before then.
At least 12 months of access
No VAT is charged.