Local & Global Variables (Cambridge (CIE) O Level Computer Science)
Revision Note
![Robert Hampton](https://www.savemyexams.com/cdn-cgi/image/f=auto,width=3840/https://cdn.savemyexams.com/uploads/2024/01/robert-hampton-computer-science-expert-save-my-exams.jpg)
Author
Robert HamptonExpertise
Computer Science Content Creator
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 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
Local variables |
---|
|
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 functionprintValue
This means that this function and any other modules can access and change the value in the global variable
Global variables |
---|
|
You've read 0 of your 0 free revision notes
Get unlimited access
to absolutely everything:
- Downloadable PDFs
- Unlimited Revision Notes
- Topic Questions
- Past Papers
- Model Answers
- Videos (Maths and Science)
Did this page help you?