Local & Global Variables (Cambridge International IGCSE Computer Science)
Exam board Cambridge International · IGCSEUpdated 2026-05-28
Local Variables
Spec point — (c) Understand and use local and global 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
Examples
- In this code, you can see that the
localVariable(with the value 10) is declared inside of the functionprintValue - 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
| Psuedocode |
|---|
| Python |
|---|
Global Variables
Spec point — (c) Understand and use local and global variables
What is a global variable?
- A global variable is a variable declared at the outermost level of a program.
- 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
Examples
- In this code, you can see that the
globalVariable(with the value 10) is declared outside of the functionprintValue - This means that this function and any other modules can access and change the value in the global variable
| Psuedocode |
|---|
| Python |
|---|
Was this revision note helpful?
Build on this topic