Skip main navigation

What is a Variable?

Variables allow you to store and manipulate data in your programs, explore this fundamental part of programming.

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.

This article is from the free online

Teaching Programming in Primary Schools

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now