Global & Local Variables (OCR A Level Computer Science)
Revision Note
Global Variables
What is a Global Variable?
A global variable is a variable declared at the outermost level of a program. This means that they are declared outside any modules such as functions or procedures.
Global variables have a global scope, which means they can be accessed and modified from any part of the program.
Python example
In this python code, you can see that the globalVariable
(with the value 10) is declared outside of the function printValue
. This means that this function and any other modules can access and change the value in the global variable.
globalVariable = 10 # Defines a global variable
def printValue():
global globalVariable # Access the global variable inside a function
print("The value into the variable is:", globalVariable)
printValue() # Call the function
Usage & need for global variables
Data Sharing: Global variables facilitate data sharing between different parts of the program, allowing data to be passed between different modules easily without the use of parameter passing.
Persistent Storage: Global variables retain their values throughout the program's execution, making them suitable for storing data that needs to persist across function calls
Global Configuration: Global variables can be used to store configuration settings or constants that are relevant across the entire program
Benefits and drawbacks
Benefits | Drawbacks |
---|---|
The global variable only needs to be declared once. | The global variables are always stored in memory while the program is running which can use up memory. |
You don’t need to keep passing parameters between the different modules. | Makes the program difficult to maintain as it’s difficult to understand where variables are changed in the program. |
| Makes it difficult to test a single block of code as the programmer will need run the entire program to setup the global variables. |
Local Variables
What is a Local Variable?
A local variable is a variable declared within a specific scope, such as a function or a code block
Local variables are accessible only within the block in which they are defined, and their lifetime is limited to that particular block
Once the execution of the block ends, the local variable is destroyed, and its memory is released
Python example
In this python code, you can see that the localVariable
(with the value 10) is declared inside of the function printValue
. This means that only this function can access and change the value in the local variable. It cannot be accessed by other modules in the program.
def printValue():
localVariable = 10 # Defines a local variable inside the function
print("The value of the local variable is:", localVariable)
printValue() # Call the function
Benefits and drawbacks
Benefits | Drawbacks |
---|---|
Local variables encapsulate data within a particular function or block, providing data hiding and preventing unintended access from other parts of the program. | Repeatedly creating and destroying local variables within a loop or recursive function can incur unnecessary memory overhead. |
Local variables enable you to use the same variable name in different functions or blocks without causing conflicts, as each local variable is confined to its own scope. | Excessive use of local variables within deeply nested blocks can lead to code clutter and reduce code readability. |
Local variables have a limited lifetime, and their memory is automatically reclaimed when the code block ends, making them memory efficient. |
|
Converting Between Global and Local Variables
In this Python code below, a global variable named
global_string
is declared and initialised with the string "Hello"Two functions are defined,
add_world
andadd_name
The
add_world
function appends the string ", world!" toglobal_string
The
add_name
function takes a stringname
as an argument and appends it toglobal_string
add_world
is called and printsglobal_string
, which results in "Hello, world!"After that,
add_name
is called with the argument "Alice" and printsglobal_string
again, which results in "Hello, world! Alice"
# Global variable
global_string = "Hello"
def add_world():
global global_string
global_string += ", world!"
def add_name(name):
global global_string
global_string += " " + name
# Call the functions
add_world()
print(global_string) # Outputs: Hello, world!
add_name("Alice")
print(global_string) # Outputs: Hello, world! Alice
In this revised Python code, a local variable named
local_string
is declared and initialised with the string "Hello"Two functions are defined,
add_world
andadd_name
The
add_world
function takes a strings
as an argument, appends the string ", world!" to it and returns the modified stringThe
add_name
function takes two arguments: a strings
and a stringname
. It appendsname
tos
and returns the modified stringadd_world
is called withlocal_string
as an argument and assigns the result back tolocal_string
.local_string
is printed, which resulted in "Hello, world!"add_name
is called withlocal_string
and "Alice" as arguments and assigns the result back tolocal_string
.local_string
is printed again, which results in "Hello, world! Alice"
def add_world(s):
s += ", world!"
return s
def add_name(s, name):
s += " " + name
return s
# Initialize a local variable
local_string = "Hello"
# Call the functions
local_string = add_world(local_string)
print(local_string) # Outputs: Hello, world!
local_string = add_name(local_string, "Alice")
print(local_string) # Outputs: Hello, world! Alice
You've read 0 of your 5 free revision notes this week
Sign up now. It’s free!
Did this page help you?