Introduction

Consider a simple program that adds 2 + 2 and outputs the result to the screen. That’s great, but what if you want to add different numbers? Do you want to have to go into the program and change the numbers that it adds? That sounds like some trouble! Also, this is a very simple program, what about complex programs? Changing numbers would not be feasible. Wouldn’t it be easier to create 2 variables that represent the first and second number (say ‘a’ and ‘b’ to store the first and the second number respectively), and get input from the user as to what the values of those variables are? Then all you have to do is instead of writing print 2+2 (or whatever the right syntax is in your programming language), you write print a+b. It couldn’t be simpler.

Definition

Variables are like a storage container. They are used to store information to be referenced and manipulated in a computer program. They also provide a way of labelling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. Their sole purpose is to label and store data in memory. This data can then be used throughout your program. Hence, they are critical to writing re-usable code. Variables can only hold one value at a time, unlike lists. These values can be either numbers or strings — any text. In SCRATCH, clicking on an isolated variable in the scripts area displays a small bubble reporting the value of the variable. Variables must be created prior to when the project actually runs.

Contrary to algebraic variables (which are usually unknown), the variables in Scratch and other programming languages are simply known values. In fact variables can contain text (strings), numbers, or Booleans (true/false values). Some examples are below:

  • Hello, world
  • 123
  • 14
  • 0
  • -321
  • true
  • {nothing — empty string}

You can create a variable by clicking on create variable button in Variable palette. This creates a new variable in the palette. You can create any number of variables you want.

Types of variables

There are two types of variables, public (global), and private (local):

GLOBAL VARIABLES

By default, when a variable is created, it is a global variable. Global variables can be read and changed by any sprite, or the Stage.

LOCAL VARIABLES

In general, local variables (or private/personal) are variables which are accessible only to specific parts of the code. They are created in the same way as global, but the option For this sprite only” is selected in the variable creation dialog. Personal variables can only be changed by their “owner”, but can be read by other sprites using the () of () block. The Stage cannot have local variables.

Local variables are extremely useful when you want to create a template sprite which needs to be duplicated and edited. For example, in a game where one must pop bubbles, a “bubble” sprite should be made which has personal variables like “speed” and should be programmed independently. Then it should be duplicated until there are enough bubbles. Since each bubble has an individual “speed” variable, they will not interfere with each other unlike if “speed” was a global variable. Consider each “speed” variable as the personal property of a bubble.

Use of a Variable

Variables are used whenever a value must be stored — i.e., if a project requires the user to input a name and then remember that name, the name would be stored in a variable. With this, the name can be retrieved at any time; all the project has to do is check the value (which is the name).

A Stage monitor is available for every variable to display the value of a variable on the stage. There are three types of Stage monitors for a variable:

  • Normal readout: The typical Stage monitor that you get when you first show it. This shows both the name and the value of the variable.
  • Large readout: This shows only the value, and in a larger font than the normal readout.
  • Slider: This lets the user manually change the value of a variable inside a range which can be defined by right-clicking the Stage monitor on the project screen and choosing two numbers.

The form of the stage monitor can be changed by double-clicking or right-clicking it and selecting the option that is wanted, or clicking it using the grow/shrink sprite tool.

Variable Display

It can be chosen whether to hide or show a variable on the stage by clicking the tick box in the variable panel or by right-clicking it and selecting “hide”.

Checkbox variable

There are five different blocks relating to variables:

 

NameBlockFunction
Set VariableThe block will set the specified variable to the given value: a string or number.
Change VariableThe block will change the specified variable by a given amount.
Show VariableThe block shows the specified variable's Stage monitor.
Hide VariableThe block hides the specified variable's Stage monitor.
VariableThe block simply holds its variable

Project

Let us use these concepts to make a project, where the Scratch cat will ask you to enter a temperature in Fahrenheit and displays the temperature in Celsius.

Given below is the conversion formula:

C = (F-32)*(5/9)

Where, C is temperature in Celsius
F is the temperature in Fahrenheit.

To start with the project, you need to make two variables Celsius and Fahrenheit and store the values. When the user enters a value you will store it in the Fahrenheit variable and when you convert the temperature into Celsius, you store it in Celsius.

Follow these steps to make the project:

  • Create two variables named Celsius and Fahrenheit.
  • Choose a hat block to start executing the program. For this case let us choose green flag.
  • It is recommended to initialise the variable with some value at the start of program, because once you complete executing the program the value at the end will be the new value of the variable, and it doesn’t get reset when you execute it again which may lead to wrong results. Hence using set block, initialise them to zero.
  • You would also want to hide the variable from the stage and once you get the result, you display it. Hide both variables.
  • To make the project catchy you can make cat say some interesting phrase to the user. For this, drag and drop ‘say’ block from looks and add your catchy lines, like “Hello, I am a cool metric cat. I can convert temperature from Fahrenheit to Celsius.”
  • Then you want the user input the temperature in Fahrenheit. In Sensing palette, there is a block named ‘ask’, which creates a dialog box for the user to enter a number or a string. The user reply can be accessed by ‘answer’ block which is just below the ask block.
    Drag and snap the ask block and write the question to the user, like “Please enter temperature in Fahrenheit”.
  • Then you store the answer that the user gives in the Fahrenheit variable.
  • Using the equation, we want to get the Celsius value from Fahrenheit. For this we will use some operator blocks from Operator palette. Using these operator, we will break the above conversion into simple operation sets, like 1 subtraction, 1 division and 1 multiplication will compute Celsius value:
    C = (F-32)*(5/9)
    Operator
    Drag and drop subtraction operator, then drag and drop Fahrenheit variable in the first input space and write 32 in the second input space.
    Drag and drop division operator, and input 5 in first and 9 in second input space.
    Now drag and drop a multiplication operator, then drag and drop the subtraction operator in the first input space and the division operator in the second input space. Hence you have evaluated the Celsius value.Store this value in the Celsius variable.
  • The cat will now say the value to the user. Add a say block and edit message to “Ah, the value is”.
    Show the value of Celsius and Fahrenheit to the user and say the Celsius value using say block again.
    This is what your script looks like

Yeah, you have completed you project and now you can start executing your script by clicking green flag.

In this tutorial we have learned what are variables, how to create them and how to use them in Scratch.  Next, we will see how to use conditional statements.

Variable example evive