Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Teaching Programming in Primary Schools. Join the course to learn more.

What is a Variable?

A variable is a piece of data stored in a computer’s memory. Each variable is given a name, so that the stored data can be accessed and changed later. The data stored in a variable can be a number, some text or even images and sounds.

Think about playing a board game with your friends. At the start of the game you may write the players’ names on a piece of paper. As the game progresses, you keep a record of each player’s score, updating the scores each time a player takes another turn.

Keeping Score

Each player’s score is a variable. The player’s name is used as the variable name. Every time they take a turn, you are updating the variable to reflect their new score.

For another example, think of storing contact details in an address book. You may use an address book to store lots of information against each of your friend’s names. This information may include their address, phone number, email address and even a photo.

The same is true for a computer. When playing a computer game, the game itself will need to keep track of the player’s score, lives, and time remaining – as well as data such as the current level and the position of various items and enemies in the game world. Lots of variables will be used to store this data, and the values stored will be updated as the game progresses.

Computer Game Variables

The data stored in these variables may be used to decide what to do next. For example, when a ‘time’ variable reaches 0 a life is lost, and when the player’s ‘lives’ variable reaches 0 then the game is over.

Share this article:

This article is from the free online course:

Teaching Programming in Primary Schools

Raspberry Pi Foundation