What is a variable? How is it used in programming? How were they able to use it in class?
What are variables?
Variables are a way to store information in a program, and they're used most often to store values. If you're playing a video game, there's a variable to store your score, another one for your lives, and maybe another one for time. With your CoDrone, you might use them to store the value of a sensor, the speed of your robot, or maybe how far it will go.
Variables are great if you have a lot of values in your program! For example, you might have a program with five different places where you set the speed. Without a variable, you would need to change all five numbers. With a variable, you would only need to change the variable's value.
Now, let's make our own variable in Snap!
Ready to make your own variable? Go to the orange variables menu and click on the gray "Make a variable" block at the top. Snap! will then ask you to name the variable. This can be whatever you want --- we just named ours "testing". Once the variable is made, it will shows up as a block in the orange variables menu.
Wondering what the check mark next to the variable is for? That controls whether you can see the variable at the top righthand corner of your screen or not.
The "set to" block will set your variable to the value at the end of the block, which you can select from the drop-down menu. To set this value, you can:
- Type in a number
- Use another variable or a block from the green operators menu (your math teacher will love this menu!)
The block will then save your values into the variable.
The "change by" block will add the value at the end of the block to your variable, which you can select from the drop-down menu. To set this value, you can:
- Type in a number to change the value. If the number is positive it will increase the value of the variable, and if the number is negative it will decrease the value of the variable.
- Use another variable or a block from the green operators menu
Put it all together
Ready to test variables in the wild? You're going to combine what you learned about variables with what you learned about starting blocks. Make a program that will set your variable to 5 when you press the green flag and add 1 to your variable every time you press the space key.
If you're not sure how to do this yet, here's the spoiler:
- Under the "when green flag clicked" block, connect the "set to" block. Make sure you choose your variable from the drop-down menu and set the value to 5.
- Under the "when key pressed" block, connect the "change to" block. Make sure you choose your variable from the drop-down menu and set the value to 1.
What can you do with variables?
Variables are a way for you to save a value so you can use it later on in your program. They are usually created and placed at the top of your program, right underneath the starting block.
Variables are great if you have a long or complex program that has a lot of values that you might need to change. Without variables, you would have to go back and change each value one by one, which leaves a lot of room for mistakes. With a variable, you can just go back up to its block at the top of the program, change the value, and then it's changed throughout the entire program!
How to change a variable's value
There might be times where you need to change one variable, but not the other. You can still do this! After you create your variable and have it in your program, go to the orange variables menu, and drag and drop the change by block where you need it. This will most likely be a block that has a variable in it. In our code, it is under the yellow wait secs blocks.
Next, select your variable in the drop down menu and type in the value that you want to change your variable by. You can also use the blocks in the green operators menu or another variable (if you have it). Just remember that if you want to have your variable's value decrease, you need to use a negative number.