Use local and global variables
Control where a variable can be accessed and reduce unintended changes between routines.
Section 8 · Programming
Exam scope and assessment notes
This sequence follows Cambridge IGCSE Computer Science 0984, 2026–2028 syllabus Version 4. It covers section 8.1 (Programming concepts) only. Pseudocode examples use the published Cambridge format; the separate 2029–2031 syllabus is not mapped here. Practice is original, self-checked and does not award an official Cambridge grade. Full timed papers, a browser-based programming workspace and human subject-teacher review remain outstanding. Cambridge states that 0984 has the same 2026–2028 subject content as 0478, with 9–1 grading. In June and November, Cambridge lists 0984 in Administrative zone 3 and for centres in Oman and the UAE. Confirm availability for your centre and exam series with Cambridge International. The separately published 2029–2031 syllabus changes to Python 3 only and removes pseudocode; this 2026–2028 route does not cover those changes.
Before you begin
- Be comfortable following an ordered algorithm and tracing variable values. Each lesson introduces the Cambridge pseudocode form it uses.
Work on paper. Try each question before revealing help, and explain why your method works.
Quick readiness check
What does scope describe?
What you will learn
- Distinguish local and global scope
- Trace a variable within a procedure
- Prefer a clear scope for maintainable code
Local variables belong to a routine
A local variable is declared inside a procedure or function and is available only there. It can be used as temporary working data without changing a same-named variable elsewhere.
Parameters also provide local names for values supplied by a caller. When the routine finishes, its local working values are no longer available to other routines.
Global variables are shared more widely
A global variable can be accessed in more than one part of a program. This can be useful for genuinely shared state, but any routine that changes it can affect other code.
Prefer local variables and parameters when possible. They make it easier to see which values a routine reads and changes.
Pause and explain
A value is needed by only one function. Which scope is usually clearer?
Worked example
A procedure calculates a temporary discount. Where should TemporaryPrice be declared if no other routine needs it?
Show the worked solution
- The value is only used inside the procedure.
- Declare TemporaryPrice within that procedure.
- This makes the variable local and limits accidental use elsewhere.
Answer Declare TemporaryPrice locally inside the discount procedure.
Try a topic test
Choose Easy, Normal or Difficult. Save your answers and open worked review after finishing.
Test this topic →From guided practice to a new situation
Write your answer and reasoning first. Use a hint only if you are stuck. The model solution is for self-checking; your written work is not automatically marked.
A function declares Result inside itself. Can the main program use Result directly after the function ends? Explain.
Give me a hint
Consider where a local identifier is in scope.
Compare my reasoning
- Result is local to the function.
- The main program is outside that scope.
- Return the result or use an explicitly shared value instead.
No; a local variable is only available inside its function. Return the value if the caller needs it.
Look for these in your work
- Scope identified
- Suitable alternative
A global Attempts variable is changed by three procedures. Give one risk and one way to reduce it.
Give me a hint
Think about hidden dependencies.
Compare my reasoning
- Any procedure can change Attempts unexpectedly.
- Use a local value or pass Attempts as a parameter where appropriate.
- Keep only truly shared state global.
The shared value can change unexpectedly; pass it explicitly or keep it local when possible.
Look for these in your work
- Risk stated
- Scope improvement
Two procedures both use a local variable named Index. Does this make them overwrite each other's Index? Explain.
Give me a hint
Each routine has its own local scope.
Compare my reasoning
- The names are local to separate routines.
- Each routine has its own value while it runs.
- They do not overwrite each other solely because the identifiers match.
No; each local Index belongs to its own routine, so the matching names are separate variables.
Look for these in your work
- Scopes distinguished
- Conclusion correct
Common mistakes
- Assuming a local variable can be read outside its routine
- Changing a global value without documenting the side effect
- Using global state when a parameter would make the dependency clear
What can you explain now?
Where can a local variable normally be used?
Compare with the explanation
Inside the routine where it is declared
Its scope is limited to that procedure or function.
After trying it yourself, choose your next review. This is your self-assessment.
Your review choice appears on Today. Sign in to sync it across devices.
Make it stick
Why are parameters often clearer than changing a global variable?
If you needed the solution, close it and solve the problem again from a blank page. A correct answer is stronger when you can explain the reason for each step.