Knowledge Organiser: Variable Scope

Part of Variable Scope ยท Section 7 of 7

Topic SummaryUnit: 3.2 ProgrammingGCSE

Key Terms

  • Scope: The region of code where a variable can be seen and used
  • Local variable: Declared inside a subprogram; accessible only within that subprogram
  • Global variable: Declared outside all subprograms; accessible anywhere in the program
  • Lifetime: How long a variable exists โ€” local variables are created when the function runs and destroyed when it ends

Must-Know Facts

  • Local variables cannot be accessed outside the subprogram they are declared in
  • Global variables exist for the entire runtime of the program
  • Local variables are more memory-efficient โ€” they are freed when the function ends
  • Parameters are local to the function they belong to
  • Best practice: use local variables wherever possible โ€” safer and tidier
  • Accessing a local variable outside its function causes a runtime error

Key Concepts

  • Declare global: global score = 0
  • Declare local: local bonus = 10 inside a function
  • Global can be read and modified from any subprogram
  • Local is created on function call, destroyed on function return

Common Mistakes

  • Trying to use a local variable outside its function: Local variables only exist inside the subprogram where they are declared โ€” accessing them elsewhere causes a runtime error
  • Assuming global variables are always better: Global variables can be accidentally modified by any part of the program โ€” local variables are safer and preferred where possible
  • Forgetting local variables are destroyed after function ends: Local variables do not retain their value between function calls โ€” a new copy is created each time the function runs
  • Confusing scope with data type: Scope (local/global) describes WHERE a variable is accessible; data type (integer/string) describes WHAT it stores โ€” these are separate concepts
Unit 2.3: Robust Programs

This topic summary covers Knowledge Organiser: Variable Scope within Variable Scope for GCSE Computer Science. Revise Variable Scope in 3.2 Programming for GCSE Computer Science with 18 exam-style questions and 10 flashcards. This topic appears less often, but it can still be a useful differentiator on mixed-topic papers. It is section 7 of 7 in this topic. Use this topic summary to connect the idea to the wider topic before moving on to questions and flashcards.

Practice questions for Variable Scope

Where can a local variable be accessed?

  • A. Anywhere in the program
  • B. Only within the subprogram where it is declared
  • C. Only in the main program body
  • D. In any subprogram that calls the original subprogram
1 markfoundation

Identify and explain the scope error in this pseudocode: function calculateTotal(price, quantity) local total = price * quantity endfunction calculateTotal(10, 5) output total

2 marksstandard

Quick recall flashcards

What is variable scope?
Where a variable can be accessed in the code
What is a global variable?
A variable declared outside all subprograms, accessible anywhere in the program

18 questions on Variable Scope: practise free

Instant marking, adaptive difficulty and spaced-repetition flashcards โ€” all aligned to your exam board.

Start revising free โ†’

PrepWise is a trading name of A3M Quality Solutions Limited, registered in England and Wales, company number 14959172. Registered office: 202 Stroud Road, Gloucester, GL1 5LA. ICO registration ZC267069.