±«Óătv

Explaining variables

Have you ever heard the person who talks about the weather on TV say that the weather is going to be ?

They mean that the weather might change from one thing to another. It might be cloudy, then sunny, and then it might rain.

Variable is another word for something that changes.

Back to top

Things that change

Some things, like the weather, change all the time and we don’t know what the next change will look like. We can keep track of the changing information using a weather map or a daily weather forecast.

Some things change by getting larger or smaller like the score in a quiz or a sports game. This variable is kept on the scoreboard.

Back to top

Familiar variables

A familiar variable is the date.

We often write this variable on our work at school and it changes every day.

Even though it changes every day, some parts of the date change more slowly.

  • The year only changes every 365 days. Except during a leap year when there are 366 days.
  • The name of the month changes after about 4 weeks and repeats after 12 months.
  • The name of the day changes every 24 hours and repeats after seven days.

We can keep track of the way the date changes using a calendar or a diary.

Back to top

Using variables in computing

We use the word variable in computing to describe a place in a computer’s memory where it stores information that changes.

The variable is a like a box or container that holds the thing that is changing. The box stays the same but whatever is inside can change or vary.

The computer can store more than one variable in its memory, as long as we give each variable its own name.

This is useful as we can use the variable name when we want to use what is stored in the box at any time.

For example, a variable can be used in the program that controls an automatic car park barrier. It can keep track of the number of cars going in and out and decide whether to let more cars in.

A variable can also be used in an alarm app on a mobile phone. It can store the different time settings for the alarm, an early start on weekdays and a bit later at the weekend, when you want a lie in!

Back to top

What does a variable look like in a computer game?

Let’s look at an example.

This simple computer game is called Dinosaurs and Doughnuts.

The has written a list of instructions called an to explain what should happen in the game.

Dinosaurs and doughnuts game algorithm

If you click on a dinosaur:

  • a point is added to the score
  • the growl sound plays

If you click on a doughnut:

  • a point is subtracted from the score
  • the error sound plays

Can you spot what part of the algorithm is something that changes as the game is played?

Back to top

What would the code look like for a variable?

The computer programmer has created a variable box and given it the name score.

The variable box shows on the game screen so the player can see what number is inside.

This is what the program might look like for the first part of the algorithm.

The instructions in the code tell the game to add one point to the score variable box when a dinosaur is clicked. It also gives the instruction to make the growling sound.

But what about if the player clicks on a doughnut? Let’s go back to our original algorithm for the game.

If you click on a doughnut:

  • a point is subtracted from the score
  • the error sound plays

Can you work out which one of these is the right code for the second part of the algorithm?

Back to top

Activities

Back to top

Computing - Dance Mat Typing. game

Build and test your computing skills with different levels of touch type challenges

Computing - Dance Mat Typing
Back to top

More on Computer science

Find out more by working through a topic