Local and global variables (Edexcel GCSE Computer Science): Revision Notes
Local and global variables
What is scope?
In programming, scope refers to the area or region of a programme where a variable can be accessed and used. Think of it like different rooms in a house - some things are only available in certain rooms, while others can be accessed from anywhere in the house.
When you create a variable, it exists and can only be used within its specific scope. This is really important because it means you can have different variables with exactly the same name in different parts of your programme, and they won't interfere with each other. They might have the same name, but they're actually completely separate variables with different values.
House Analogy: Just like a house has different rooms where certain items belong, your programme has different "rooms" (scopes) where variables can exist. Some variables are private to one room, while others are accessible throughout the entire house.
Local scope and local variables
Local scope describes the area within a single subprogram (like a function or procedure). When you create a variable inside a subprogram, it becomes a local variable.
Here are the key things to remember about local variables:
- They are only accessible within the subprogram where they were created
- Once the subprogram finishes running, the local variable disappears
- Other parts of your programme cannot see or use these variables
- This helps prevent accidental changes to variables from other parts of your code
Bedroom Analogy: Think of local variables like items in your bedroom - only you can access them when you're in that room. Once you leave the room, those items are no longer available to you in other parts of the house.
Global scope and global variables
Global scope covers the entire programme that's running. Global variables are created in this wider scope, which means they can be accessed from anywhere in your programme.
Key points about global variables:
- They can be accessed from the main programme and all subprograms
- They exist for the entire duration of your programme
- Any part of your code can read and modify these variables
- They're useful for storing information that multiple parts of your programme need to share
Kitchen Analogy: Think of global variables like the kitchen in your house - everyone in the family can access it from anywhere. The kitchen resources are available to all rooms and all family members.
How variable lookup works
When your programme encounters a variable name, it follows a specific rule to decide which variable to use:
Critical Rule: Local scope is always checked first!
This means:
- The programme first looks for the variable in the current subprogram (local scope)
- If it doesn't find it locally, then it looks in the global scope
- This prevents local variables from being accidentally overwritten by global ones
Code example breakdown
Let's look at how this works in practice:
Worked Example: Local vs Global Variables
# Global Variables
theNumbers = [1, 2, 3, 4, 5]
total = 154
# Subprogram
def doAvg(pNums):
avg = 0.0
total = 0
for number in pNums:
total = total + number
avg = total / len(pNums)
return (avg)
# Main Programme
print(doAvg(theNumbers))
print(total)
Step-by-step breakdown:
- total = 154 is a global variable - accessible from anywhere in the programme
- Inside the doAvg() function, total = 0 creates a local variable with the same name
- When the programme uses total inside the function, it uses the local version (starts at 0)
- When print(total) runs in the main programme, it uses the global version (154)
- The local total only exists while the function is running
Exam tips
- Always identify the scope first when analysing variables in code
- Look carefully at where variables are declared - inside a function = local, outside = global
- Remember the lookup rule - local scope is checked before global scope
- Watch out for same-named variables - they can exist in different scopes without conflicting
- Practice tracing through code to see which version of a variable is being used at each point
Real-world applications
Understanding scope is crucial for:
- Game development - player stats (global) vs temporary calculations (local)
- Web applications - user session data (global) vs page-specific variables (local)
- Mobile apps - app settings (global) vs screen-specific data (local)
Key Points to Remember:
- Scope determines where a variable can be accessed in your programme
- Local variables only work within their subprogram - like items in a private room
- Global variables can be accessed from anywhere - like shared family resources
- Local scope is always checked first when the programme looks for a variable
- Same-named variables can exist in different scopes without interfering with each other