What is a variable and how do computers use them?

Part ofComputing Science and Digital LiteracyComputing scienceYear 3Year 4

What is a variable?

A cartoon boy thinking

Variable is another word for something that changes.

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

They mean that the weather might change from one thing to another.

It might be cloudy, then sunny, and then it might rain.

A cartoon boy thinking
Back to top

Things that change

A weather forecast showing "Fri 18 degrees rain", "Sat 21 degrees sun" Sun 18 degrees cloudy and Mon t2 degrees snow and three children behind a plinths

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.

A weather forecast showing "Fri 18 degrees rain", "Sat 21 degrees sun" Sun 18 degrees cloudy and Mon t2 degrees snow and three children behind a plinths
Back to top

Familiar variables

a cartoon calendar with the word "September" at the top

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.

a cartoon calendar with the word "September" at the top
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.

Here are a couple of examples:

Image gallerySkip image gallerySlide1 of 2, A cartoon blue car with a red and white barrier in front of it, 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.
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 computer programmer has written a list of instructions called an to explain what should happen in the game:

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.

Pixelated text saying "SCORE 001"

This is what the program code 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.

A green coding tab with the words "When Dinosaur clicked", with an orange coding tab underneath saying "change Score by 1" then a purple coding tab underneath that saying "play sound Growl until done"

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

Quiz

Back to top

Dance Mat Typing game! game

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

Dance Mat Typing game!
Back to top

More on Computing science

Find out more by working through a topic