Coding 5: Variables

Variables

Coding 5

This lesson will help you earn points in the following judging rubric lines: "App Function" and "Code Complexity".

In this lesson, you will learn...

  • What variables are and how to use them

Key Terms

  • Variables - a piece of data that can change
  • Global Variables -  variables that can be used anywhere in your code
  • Local Variables - variables that can only be used inside certain functions

Variables

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. First you would grab this block from the variables section.

global name

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.

global score 0

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 label.

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.

Reflection

  • 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 in 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.

getting global variables

 

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. The local variable will only beable 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.setting local variables