If you’ve been using Scratch for a while now, you may notice that there’s things called variables in Scratch. This is a quick tutorial on what a variable is and how we use them in scratch!

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. Being able to hold this bit of information allows us to reference and manipulate it at many different places in a program. This ability makes variables incredibly useful!

Variables are like containers that can hold a number (numerical variables) or a word (alphanumerical variables):

 

Numerical and alphanumeric variables: containers for 

 

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 that 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: do you choose for all sprites, or do you choose and for your sprite only?

 

For all sprites or for this sprite only: the true dilemma.

 

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:

 

We can create two global variables — rand1 and rand2 — to store the values of two different randomly-generated numbers.

 

Next, we must set the value of these variables to be random numbers every time the flag button is clicked:

 

Here, we give randomised values to our global variables rand1 and rand2.

 

We can see in our code that we have created two variables: one called “rand1”, and the other 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 given a random number between 1 and 10 to store!

Next, we need Scratch Cat to tell us what these two numbers are.

Try #1

First try, we can do this using the following code:

 

This code will allow Scratch Cat to say both values of rand1 and rand2 in the same say block.

 

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 so legible.

 

Here is one instance of what happens when we use the code in the previous screenshot.

 

In the above screenshot, we can see Scratch Cat telling us the value of rand1 (which is 1) and rand2 (which is 10). But it’s not very legible, is it? Let’s write some code to make it a little bit more legible.

Try #2

Let’s try this by putting the numbers into a sentence.

 

This code will ensure that rand1 and rand2 are discernible and distinguishable when Scratch Cat reports their value.

 

Now our total code should look something like this:

The full program

 

Scratch Cat can now tell us which number is which!

But why did we use this code instead of simply doing this? —

 

An alternate solution — or is it?

 

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 of the program, 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 in Scratch to achieve these!

 

Recap

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.

 

So that’s how we use variables in scratch!

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!

Happy Scratching!

 

Check out our free teacher download. Learn to code with Scratch in 30 minutes.


If you’re interested to find out more