Variables activities

All computer programs need to deal with data. In Sense data comes in the form of either strings (sequences of characters) or numbers. For example, data could be simple numbers such as temperature readings, or strings of characters such as the names of bank account holders. If a program running on a computer is going to be able to make use of this data, the data needs to be stored in the computer's memory and in order to access the stored data, the program needs some way of keeping track of where the data is stored. To help it to do this a program can make use of variables. A variable is simply a named 'chunk' or block of the computer's memory where data is stored. By using the name of the variable in program code we are able to access the data stored in the corresponding block of memory and also to change what is stored there. It is because the value that is stored in a memory location can change that we use the word variable for these named memory locations.

We can build a small program in Sense to demonstrate the basic use of variables.

Using Sense write a very small program which uses variables by following the steps given below (you can also download the program pre-written to open in Scratch). The program simply performs a basic addition - adding 10 to 90 to give 100. This may not be very interesting but demonstrates how values can be held and manipulated in a program.

Let's quickly make an adjustment to the Stack so that it performs the same function but uses just two variables rather than three. We can do that by putting the result of the addition back into one of the original variables - the 'First number' variable for example. This will save some space as we then use one less variable but it also demonsrates how the value of a variable, any variable, can be changed.

To do this simply click on the 'set' part of the final statement and change it from 'Result' to either 'First number' or 'Second number'. You can then click 'Delete a variable' on the variables pallete and select 'Result' or simply uncheck the tick next to 'Result' so it is not displayed. Then run the Stack again. The value of 'First number' is first set to 90 and then replaced by the result of the additon, so it becomes '100' (see Figure 7).

addition using two variables
Figure 7 Addition stack after being run using two variables.

So far we have only encountered Scratch variables being used to hold numbers. Of course there are several different types of data that we might want to handle in a program. Most programming languages require that you say what kind of data a variable will hold when you create the variable. This is not required in Scratch.

To demonstrate the use of variables to hold another type of data we can alter the program to handle some small texts, usually called 'strings' in a programming context.

The program, which you can download or build yourself, uses a couple of Blocks we have not used earlier. The complete program is shown in Figure 8.

Stack to join strings
Figure 8 Stack to ask your name and join the response to "Hello ".

You can assemble it in a straightforward fashion but it deserves some explanation:

Overall this program is much like the earlier addition. It takes two strings, (one of which is "Hello ", the other is taken from your response to "What's your name?") and joins them together (rather than adding as performed with numbers) and then displays the resulting single string in a final pop up box.

Run the program and type your name when asked into the dialog box (Figure 9).

Dialogue box with name typed in
Figure 9 Dialogue box with 'Nick' as the name typed in.

The response is shown in Figure 10.

Sprite making response
Figure 10 Sprite response with strings joined into one string.

That concludes the adventures using variables. Please go back and continue onto the next activity.