(MAD) Global and Local Variables Lesson
Global and Local Variables
We know that variables are containers that hold values. There are two types of variables that we can use in app inventor: global and local.
Global vs Local Variables
Global Variable
A global variable is a variable that can be accessed in multiple scopes. This means that wherever you are in the program you can use that variable; get its current value or set its value to something else. Global variables are created using the initialize global name to block found in the Variables drawer.
Local Variable
A local variable is a variable that is declared within a function or it is an argument passed into a function. This means that you can only access these variables in that specific function where they are declared or passed in as an argument.
Variable Blocks
There are five main types of variable blocks:
- initialize global name to
- get
- set
- initialize local name to in (do)
- initialize local name to in (return)
Naming Conventions for Variables
We will follow the same naming conventions for variables as we did in the first half of the course.
- Variables begin with a lowercase letter
- Variables can only contain a _ or a $ symbol
- Variables can contain numbers.
- The variable name should reflect what it stores
[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION