AQA 7517, section 4.1.1.13
A subroutine can declare its own variables. These are called local variables. Parameters also behave as local variables.
A local variable:
- exists only while the subroutine is executing. It is created when the subroutine runs and removed when it finishes;
- is accessible only within the subroutine. Code outside it can't see or use the variable.
def make_total(prices):
total = 0 # local variable
for p in prices:
total = total + p
return total
print(make_total([2, 3, 5])) # 10
print(total) # NameError: total doesn't exist here
The last line fails because total belongs to make_total and is gone once the function returns. The scope of a variable is the part of the program where it can be used. A local variable's scope is its subroutine.
Same name, different variables
Two subroutines can both use the name total without interfering, because each has its own separate local variable.
def add_up(items):
total = 0
for i in items:
total = total + i
return total
def count_chars(words):
total = 0 # a different variable
for w in words:
total = total + len(w)
return total
Even if the main program also has a variable called total, the one inside each subroutine is a separate variable that happens to share the name.
Why using local variables is good practice
- No unwanted side effects. A subroutine can't accidentally change a variable used by another part of the program.
- Names can be reused. You don't need to invent a unique name for every variable across a large program.
- Subroutines are self-contained. They can be tested and reused in other programs without relying on anything outside.
- Easier to debug. A local variable can only be changed by code inside that subroutine, so there are fewer places to look.
- Saves memory. The memory is released when the subroutine ends. Local variables are stored on the call stack, covered in 4.1.1.15.
Worked example
Task: show that a local variable doesn't change a variable with the same name outside the subroutine.
def bonus():
score = 100 # local to bonus
score = score + 50
print("Inside:", score)
score = 10 # a different variable
bonus()
print("Outside:", score)
The output is:
Inside: 150
Outside: 10
The score in bonus is created fresh when bonus() is called and is discarded when it finishes. The score set to 10 in the main program is a separate variable and is not changed.
Exam tip: if you're asked why local variables are good practice, link it to a benefit such as "the subroutine can't accidentally change data used elsewhere, so it is easier to test and reuse". Avoid just saying "it is neater".
Check yourself
When does a local variable exist?
Only while the subroutine it belongs to is executing. It is created when the subroutine is called and removed when the subroutine ends.
Where can a local variable be accessed from?
Only from within the subroutine in which it is declared.
Give two reasons why it is good practice to use local variables
Any two of: they prevent subroutines changing each other's data by accident; the same names can be reused in different subroutines; subroutines are self-contained and reusable; they are easier to debug; the memory is freed when the subroutine finishes.