Local & Global Variables (Cambridge (CIE) IGCSE Computer Science)
Revision Note
Written by: Robert Hampton
Reviewed by: James Woodhouse
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.
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 |
---|
|
Last updated:
You've read 0 of your 10 free revision notes
Unlock more, it's free!
Did this page help you?