Global vs Local Variables
Local Variables
So far, we’ve declared variables inside a function block—the indented portion of code that follows declaration of a function—which means they are local variables. Only the function declaring a local variable can see or modify it. Also, a local variable only exists while the function that declares it is using it. After that, it gets returned to unallocated memory so that another function (like loop) could use that memory for a different local variable.
Global Variables
If your program has to give more than one function access to a variable’s value, you can use global variables. To make a variable global, just declare it outside of any function, preferably immediately after your import statements. Then, all functions in the program will be able to modify or retrieve its value.
Example: store_change_retrieve_global
If you want to permanently change the global variable from within the function, you must use the keyword global to call upon the global variable. In the script store_change_retrieve, k is created as a global variable with the value 5 assigned. Each time k is called upon in the function add_k it gets incremented by 1.
- Enter, name, save and flash the script store_change_retrieve_global to your micro:bit.
#store_change_retrieve_global from microbit import * k = 5 def add_k(value): global k k += 1 value = value + k return value display.scroll(add_k(3)) display.scroll(add_k(3))
- The display will first scroll 9, then 10. Do you understand why?
The next example script declares a global variable called k, assigns a value to it, and then uses its value from within a function. In contrast, the variable called value is a local variable and only exists within the add_k function. If you try to display.scroll(value), you will receive an error.
- Enter, name, save, and flash the script store_retrieve_global to your micro:bit.
#store_retrieve_global from microbit import * k = 5 def add_k(value): value = value + k return value display.scroll(add_k(3))
Example: store_change_retrieve_global
If you want to permanently change the global variable from within the function, you must use the keyword global to call upon the global variable. In the script store_change_retrieve, k is created as a global variable with the value 5 assigned. Each time k is called upon in the function add_k it gets incremented by 1.
- Enter, name, save and flash the script store_change_retrieve_global to your micro:bit.
#store_change_retrieve_global from microbit import * k = 5 def add_k(value): global k k += 1 value = value + k return value display.scroll(add_k(3)) display.scroll(add_k(3))
- The display will first scroll 9, then 10. Do you understand why?
If you want to permanently change the global variable from within the function, you must use the keyword global to call upon the global variable. In the script store_change_retrieve, k is created as a global variable with the value 5 assigned. Each time k is called upon in the function add_k it gets incremented by 1.
- Enter, name, save and flash the script store_change_retrieve_global to your micro:bit.
#store_change_retrieve_global from microbit import * k = 5 def add_k(value): global k k += 1 value = value + k return value display.scroll(add_k(3)) display.scroll(add_k(3))
- The display will first scroll 9, then 10. Do you understand why?