Local Variables (AQA GCSE Computer Science): Revision Notes
Local variables
What are local variables?
Local variables are variables that are created and used inside a subroutine (also called a function or procedure).
Think of them like items in your bedroom - they belong to that specific room and can't be used anywhere else in the house!
The key thing about local variables is that they have local scope. This means they only exist within the subroutine where they're defined and cannot be accessed by the main programme or other subroutines.
How local variables work
When you create a variable inside a subroutine, it becomes a local variable. Here's what happens:
Local Variable Lifecycle:
- The variable is created when the subroutine starts running
- It can only be used within that subroutine
- When the subroutine finishes, the variable is destroyed
- The main programme cannot "see" or use these variables
This is different from global variables, which can be accessed from anywhere in the programme.
Understanding variable scope
Scope refers to where in your programme a variable can be accessed and used.
Local variables have a very limited scope - they're like having a conversation in a soundproof room where nobody outside can hear what you're saying.
In the example subroutines we might see, variables like area and pi exist only within the circle calculation subroutine. They do their job inside the subroutine and then disappear when the subroutine ends.
Worked Example: Variable Scope in Action
def calculate_circle_area(radius):
PI = 3.14159 # Local variable - only exists here
area = PI * radius**2 # Local variable - only exists here
return area
# Main programme
result = calculate_circle_area(5)
print(result) # This works - we get the returned value
# print(PI) # This would cause an error - PI doesn't exist here
# print(area) # This would cause an error - area doesn't exist here
The variables PI and area only exist within the calculate_circle_area subroutine.
Common errors with local variables
One of the most frequent mistakes students make is trying to access a local variable from outside its subroutine.
Common Mistake: Accessing Local Variables from Outside
def circle_area(radius):
PI = 3.14159
area = PI * (radius**2)
return area
print(circle_area(10)) # This works fine - prints the result
print(area) # This causes an ERROR!
The error message you'll see is something like: NameError: name 'area' is not defined
This happens because the variable area only exists inside the circle_area subroutine. Once the subroutine finishes, the variable is no longer available.
Why use local variables?
Local variables are actually really helpful for several reasons:
Organisation: They keep your code tidy by ensuring variables are only used where they're needed. This prevents confusion and makes programmes easier to understand.
Safety: Since local variables can't be accidentally changed by other parts of your programme, they make your code more reliable and less prone to bugs.
Memory efficiency: Local variables are automatically cleaned up when the subroutine ends, which helps your programme use memory more efficiently.
Reusability: You can use the same variable names in different subroutines without them interfering with each other.
Exam tips
Essential Tips for Success:
- Remember: If you try to access a local variable from outside its subroutine, you'll get an error
- Practice: Try writing simple subroutines and identify which variables are local
- Check your scope: Always ask yourself "where can this variable be accessed?" when writing code
- Use parameters: If you need to get data into a subroutine, use parameters rather than trying to access local variables from outside
Remember!
Key Points to Remember:
- Local variables only exist within the subroutine where they're defined
- Attempting to access a local variable from outside its subroutine causes a "name not defined" error
- Local scope means the variable can only be "seen" and used within its own subroutine
- Local variables help keep code organised, safe, and efficient
- Use parameters to pass data into subroutines instead of trying to access local variables from outside