What is a variable?
As a quick recap from a few blog posts ago, a variable is a container of sorts that can hold one piece of information at a time, like a word or a number. Holding this bit of information allows us to reference and manipulate it in many different places in a program, which is why variables are so great!
In our previous blog post, we talked briefly about how to create a variable, but we didn’t talk about one small detail. Recall that, when you’re making a variable, you can choose to create it for all sprites or for your active sprite only. The option you select will determine whether you’re making a global or a local variable.
What is the difference between a global variable and a local variable?
So, you’ve come to the crossroads of choosing between for all sprites and for your sprite only. If you select for all sprites, your variables become global, meaning that it can be changed or accessed from any sprite in your project, regardless of the on which sprite it was created. On the other hand, if you choose for this sprite only, your variable will become local. A local variable is one that can only be changed or accessed from the sprite on which it was created.
For example, the internet is global and anything saved online can be accessed using any computer in the world! However, if you save something to the hard drive of your computer, you won’t be able to access it on a different device as it was saved locally.
Now that we know the two different types of variables we can have, let’s move on to putting them into action!
How do we use variables in Scratch?
To explore how to use variables in Scratch, we’re going to make a program that generates two random numbers. Scratch Cat will then report what these two randomly generated numbers are and tell us if they are a match!
Let’s start small.
First, we want to create two variables to store these two randomly generated numbers:
Next, we must set the value of these variable to be random numbers every time the flag button is clicked:
We can see this in our code, as we’ve created two variables, one called “rand1” and one called “rand2”. Both of these variables have been set to pick a random number between 1 and 10. Therefore, when the green flag is clicked, both variables will be set to a random number between 1 and 10!
Next, we need Scratch Cat to tell us what these two numbers are. We can do that using the following code:
By joining the variables “rand1” and “rand2” it means that both will be said by Scratch Cat when placed inside the say for 2 secs block. However, this code will simply print the two numbers side by side, which isn’t as legible as it would be if we put the numbers into a sentence.
To make a sentence, we need to add some more join blocks so that we can join “Number one is” and “rand1”, and join “and number two is” and “rand2”, and then join the two join blocks together and put it back in the say for two secs block.
Now our code should look something like this:
Scratch Cat can now tell us which number is which!
But why did we use this code instead of simply doing this? —
Yes, these two pieces of code produce the same result, but in the second example, the random numbers that are generated aren't saved. So, yes, they exist in that one instance, but if you want to use these same numbers at some other point in the program, you can't — because you didn't save their information in a variable!
So, if for example, we were to extend the program by getting Scratch Cat to compare the numbers and see if they are the same instead of stating their values. What if we want Scratch Cat to report it when we find a match? What if we want to keep track of the number of times the two random numbers match? We need variables to achieve these!
So, let’s recap what we’ve learned today:
There are two different types of variables, global and local
Global variables can be altered and used by all sprites, whereas local variables can only be edited and used by the sprite they were created on
We use variables when we want a bit of information to be saved so we can use it in multiple places in our program
If you’re interested in giving yourself an extra challenge, try out some of the extensions of the program we suggested, such as:
Try to extend the program so that the two random numbers are compared.
Add a new variable — score — to keep track of the number of times the two randomly generated numbers have the same value.
Check back soon to see the solutions in our next blog post where we’ll be extending our discussion on variables by talking about lists!
By Jocelyn Glencross