Global and local variables (2.2.1)
- 20p13280
- Oct 21, 2025
- 3 min read
This post covers the following from the specification (2.2.1):
(a) Programming constructs: sequence, iteration, branching.
(b) Recursion, how it can be used and compares to an iterative approach. (Skipped for now)
(c) Global and local variables.
(d) Modularity, functions and procedures, parameter passing by value and by reference.
(e) Use of an IDE to develop/debug a program.
(f) Use of object oriented techniques. (Skipped for now)
What is scope?
Scope is the area in which a variable can be accessible. There are 2 main types of scope. Global and Local.
Local Variables
Local variables are variables that are defined within a subroutine and are only accessible from within the subroutine, hence the name local scope. Once the subroutine has finished running, the local variables created within it are destroyed. They are very helpful and have many usecases, some being:
Since local variables can't be accessible anywhere else in the code it means that other subroutines can use the same identifier for their variables. So instead of having to have variables like score1,score2,score3, etc. you can just re-use the score identifier.
Adding on, since they can't be accessed from anywhere but the subroutine it means using them avoids running into a logic error caused by setting the wrong variable within a subroutine.
Local variables also save memory, once they are done with they are destroyed compared to a global variable which won't be. This means that local variables are more memory efficient.
Here is an example of using local variables:
def reverse_string_and_append(input, to_append):
reverse_input = input[::-1]
to_return = reverse_input + to_append
return to_return
to_reverse = input("Enter a string to reverse: ")
to_append = input("Enter a string to append to the reverse: ")
new_string = reverse_string_and_append(to_reverse,to_append)
print(new_string)In this example both local variables and global variables are being used. The subroutine reverse_string_and_append() takes in 2 parameters, input and to_append. These are local variables as they are only accessible within the subroutine and are destroyed once the subroutine has returned to reversed appended string. Within the subroutine 2 other local variables are defined, reverse_input and to_return. Again they are both only accessible from within the subroutine and they both are destroyed once to_return is returned.
Outside of the subroutine if you try to access any local variable like to_return it would throw an error. In python this would be a NameError and it would say that the variable is not defined as it has been destroyed since the subroutine has finished running.
Global Variables
Global variables are variables that can be accessed from anywhere in the program, hence the name global scope. This can be useful for variables that need to be accessible all throughout the program such as constants. Global variables are typically defined outside of a subroutine, however in languages such as python there is the "global" keyword. Using this you can also define global variables within subroutines.
Here is an example of both local and global variables being used (same as the local example):
def reverse_string_and_append(input, to_append):
reverse_input = input[::-1]
to_return = reverse_input + to_append
return to_return
to_reverse = input("Enter a string to reverse: ")
to_append = input("Enter a string to append to the reverse: ")
new_string = reverse_string_and_append(to_reverse,to_append)
print(new_string)In this program, there are both local and global variables. There are 3 global variables within this program -> to_reverse, to_append and new_string. These variables can be accessible all throughout the program. This means that the reverse_string_and_append subroutine could be turned into a proceedure without parameters and utalise global variables, that would look someting like this:
def reverse_string_and_append():
reverse_input = to_reverse[::-1]
global new_string
new_string = reverse_input + to_append
to_reverse = input("Enter a string to reverse: ")
to_append = input("Enter a string to append to the reverse: ")
reverse_string_and_append(to_reverse,to_append)
print(new_string)Within this new example the subroutine now uses the existing global variables from the input and creates its own to store the value it would have returned. However this isn't always the best approach.
Using global variables here means that the variables are accessible from everywhere in the code, so if this program was longer there is a good possibility that you might accidentally overwrite one of the global variables like to_reverse, to_append and new_string. This would then cause a logic error as the program won't output what the programmer expected.


Comments