Global & Local Variables (OCR A Level Computer Science): Revision Note
Exam code: H446
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_stringis declared and initialised with the string "Hello"
- Two functions are defined, - add_worldand- add_name
- The - add_worldfunction appends the string ", world!" to- global_string
- The - add_namefunction takes a string- nameas an argument and appends it to- global_string
- add_worldis called and prints- global_string, which results in "Hello, world!"
- After that, - add_nameis called with the argument "Alice" and prints- global_stringagain, which results in "Hello, world! Alice"
# Global variableglobal_string = "Hello"
def add_world():    global global_string    global_string += ", world!"
def add_name(name):    global global_string    global_string += " " + name
# Call the functionsadd_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_stringis declared and initialised with the string "Hello"
- Two functions are defined, - add_worldand- add_name
- The - add_worldfunction takes a string- sas an argument, appends the string ", world!" to it and returns the modified string
- The - add_namefunction takes two arguments: a string- sand a string- name. It appends- nameto- sand returns the modified string
- add_worldis called with- local_stringas an argument and assigns the result back to- local_string.- local_stringis printed, which resulted in "Hello, world!"
- add_nameis called with- local_stringand "Alice" as arguments and assigns the result back to- local_string.- local_stringis 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 variablelocal_string = "Hello"
# Call the functionslocal_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
Unlock more, it's free!
Did this page help you?

