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

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education