Variables

Changeable Placeholders

The attributes we use to describe Scratch the Cat’s location and orientation are called variables.

Wait a minute! We’ve seen that word before in countless contexts! Let’s recall two forms of variables:

Variables in Algebra I

Remember Algebra I and its most famous denizen, x? That li’l guy is everywhere from

2x + 3 = 15
to

Its primary purpose is to act as a placeholder for a value that was unknown. The point of most Algebra I problems is to figure out just exactly what x is a placeholder for—a kind of a mathematical hide-and-seek game.

Variables in a Science Fair Project

The Science Fair always ends with a cafeteria full of tri-fold cardboard displays, but a good science fair project always begins with a hypothesis and an experiment to test it. This picture is the beginning of such an experiment; plants were given distilled water, tap water, brackish water, and salt water for daily waterings. The hypothesis might be that the purer the water, the better the growth. The experiment is designed to test that conjecture.

This is done by establishing controls and variables. If you want to make sure that any difference you see in plant growth is due to the type of water you give them, you have to make sure that they receive equal treatment in every other possible way—the same amount of sunlight, the same type of soil, etc. These constant factors are controls, whereas the water is the variable.

In this sense, a variable is a quantity (or quality) that changes. In other words, what it represents in one scenario may be completely different in another.

Programming Variables

Variables in computer programs are similar to both of the types of variables previously discussed—they are both placeholders and changeable quantities. Let’s look at examples for each characteristic:

Placeholder

Scratch the Cat is minding his business when suddenly a voice from above tells him turn ← [15] degrees! We know from examining his attributes that means subtract 15 degrees from his direction. Or, perhaps the voice tells him move [10] steps. This one is more complicated because it involves modifying both x positionand y positionand it depends on which direction he is facing. However, to some degree, it doesn’t matter, because the variables are an abstraction. Essentially, turn ← [15] degrees means subtract 15 degrees from directionwhatever it is. The variable direction acts as a placeholder.

Changeable Quantity

Consider direction in the example above. As you rotate Scratch the Cat, the direction variable changes. Note how this is different than variables in algebra equations. In 2x + 3 = 15, x is always 6. There is one correct answer for “what is x?” However, unlike algebra equations, computer programs are dynamic things; they change over time. Scratch the Cat moves, turns, and meows. All of this possible because we are able to change the variables that define him.

Try It Out!

Load the Scratch program from User input and interaction (and Show Me Your State), and edit it as described below.

Check the box next to direction in the Motion tab. Now, an indicator will appear on the screen with the label direction and a value. Continually click the turn ← [15] degrees block and track how the direction changes. In the information pane for the sprite, you can freely rotate it. Notice how direction is updated. Clicking on turn ← [15] degrees once more will still work as intended, because it merely subtracts 15 degrees from directionwhatever it happens to be.