This lesson will help you earn points in the rubric lines for the Demo Video.
In this lesson, you will learn...
- What variables are and how to use them
One of the most important concepts in coding is a variable. A variable is a name for a piece of data that can change. An easy way to remember is that a variable can vary, or change in value.
There’s lots of information that is important to you every day that changes value. Here are some examples:
- Your age
- The weather
- Today’s date
Picture a variable as a box that you can store some information inside of and put away. Before you put it away, you put a name on the box to remember what is inside of it. When you need the information you can go find the box get what if inside. You can also change what is inside the box but keep the same name on it.
Variables are a way for your app to remember something. Just like the box, your variables will need a couple of things.
- A name so that your app can find it
- Some information to store
Variables are useful anytime you need your app to “remember” some information and when that information might change. Here are some examples:
- A player’s score in a game
- A person’s answer to a quiz question
- Things that are in someone’s shopping cart
Example: How to use variables to store the score in a game in App Inventor
To better understand variables and how to use them, let’s walk through an example. Imagine you are creating a game where the user can earn and lose points. You want the player to be able to see their score, so you put the score into a label. At the start of the game, the player has zero points so you added a block like this to the game.
However, the player’s score will change as she plays the game. Each time she gains or loses points you have to update the text box. When she gets more points, you add a block like this.
That works, but now what about the next time the player earns 10 points? You’ll have to update the text again to say 20. Then what if the player loses points? You will have to change her score to 15. What if she gains points again? This can get confusing really fast.
In scenarios like this, you can create a variable to keep track of the player’s score. You can create a variable called score that always has the value of the player’s current score. As she plays the game, you can add and subtract points from score and you won’t need to worry about keeping track of what her current score actually is. Here’s how it would work in App Inventor and Thunkable. First you would grab this block from the variables section.
This block tells the app that there will be a variable and asks you to give the variable a name and a value. Here’s what the block would look like for a game where the player’s score started at zero.
With this block, you are telling the app that there will be a piece of data called score and that it will change value as the game is played. Every time the app starts up, it is going to create a variable called score and set it zero. Now you can do what’s called “set” and “get” your score variable. “Get” will allow you to see what the value of the score is. Here’s how you would tell your app to display the score in a textbox.
“Set” allows you to assign a new value for your user’s score. Every time the player earns points you can tell the app to do this.
This tells your app to change the value of the score to whatever it is now and add ten to it. If the current score is zero, the new score would be 10. If the current score is 25, the new score would be 35. Now you don’t have to worry about assigning the score every time your player gains or loses points. The app will just take the current score and add 10 to it.
It is okay if you are still confused about variables! They are really hard to understand and the best way to understand them is to practice using them. You will get practice using them in the next few coding lessons and coding challenges.
Activity: Counting App
This is an app that allows you to count by 1, 5 and 10.
However, it is incomplete! The counter can never start over. Fix this app so that you can reset your counter and start over from zero.
- What are other variable that you can identify in your life?
- How would you use variables in your app?
- How can variables benefit your app?
Additional Resources: Local and Global Variables (App Inventor) and App, Stored, and Cloud Variables (Thunkable)
App Inventor and Thunkable each treat variables a little differently, so just read the section for whichever platform you are using to code your app.
Local and Global Variables - App inventor
You may have noticed in the section above that the variable block said “global”. There are two types of variables, local variables and global variables. When you create variables, you’ll have to think about whether you want to use the variable at multiple places in your code, such as the score variable from above, or if you want to use it in just one place.
Global variables can be used anywhere. Like you saw above, the score variable needed to be used multiple times. You had to use it when the player earned points, when the player lost points, and to display it in the textbox. It could also be used for a high-score table. You can never reuse a name when using a global variable. If you did, your app won’t know which variable to use. Luckily App Inventor won’t allow you to create two variables with the same name. If you’re in doubt of which variable type to use, use global. One thing to remember is that each screen in App Inventor has to have its own variables. A global variable can be accessed anywhere within a screen.
Local variables can only be used at one spot in your code. If you need a variable to keep track of something in just one place, use a local variable. For example, you might want to use a variable just for a Button.Click event, or inside a procedure that you have made. The local variable will only be able to be used inside the “local variable” block and App Inventor won’t let you use it outside of it. Unlike global variables, you can reuse the names of local variables.
In the coding challenge, you’ll see examples of when to use global variables and when to use local variables. To learn more about global and local variables visit MIT’s website: Global and Local Variables.
App, Stored, and Cloud Variables - Thunkable
Thunkable treats variables differently than App Inventor. It has three categories of variables.
...are similar to global variables in App Inventor. They can be used anywhere in the app, and on any screen. Like you saw above, the score variable needed to be used multiple times. You had to use it when the player earned points, when the player lost points, and to display it in the textbox. It could also be used for a high-score table.
You can never reuse a name when using a global variable. If you did, your app won’t know which variable to use. Luckily Thunkable won’t allow you to create two variables with the same name. If you’re in doubt of which variable type to use, use an app variable.
...allow you to store a variable on your device. When you close an app on your device, all variables are wiped from the memory of the device. So the next time you open the app, the variable is recreated. That is why you add a variable to your app using the initialize variable block. It initializes the variable to whatever starting value you give it.
Stored variables do not get wiped when you close the app. It stores the current value on the device, and when you reopen the app, it retrieves the value. Note that the initialize block for stored variables does not require a starting or initial value. The initial value is retrieved from the device. Of course, if it is the first time you are running the app, you will need to set the value for the variable somewhere in your app.
...are similar to stored variables, except the variables are stored in the cloud. To use cloud variables, you will have to add a Firebase component to your app. Your cloud variables will be stored in the Firebase database. When the user closes and reopens the app, the current value of the variable will be retrieved from Firebase.
For more information on Thunkables different variable types, visit the Thunkable website, Variables.