What is a variable?

The word vary means to change or differ.
Other more familiar words that are linked to vary include variety, varies, various and variable. They all mean something to do with change and difference.
We find variableA named piece of data (often a number or text) stored in a computer’s memory, which can be accessed and changed by a computer program. everywhere around us in our everyday lives. The world is always changing!

Variables in everyday life
Let's think about a supermarket checkout.
- When a new shopper gets to the checkout the screen display reads £0.00.
- Each item is put through the barcode scanner to find the price.
- The price will change or vary depending on the item.
- The price is added to the total cost displayed on the screen.
- The total cost displayed changes or varies every time a new item is scanned.

Variables in a computer program
In computing, we use the word variable to mean a specific part of a computerprogramA sequence of instructions written in a coding language that a computer can understand. that can store a piece of information that changes or varies when the program is run.
We can think of the variable like a box that holds the changing information. The box stays the same but the information inside it can change.
Let's think about the algorithmA precise set of ordered rules or instructions that can be followed by a human or a computer to achieve a task. for the computer program that is controlling the checkout till display.
We know there are two things that are changing.
- The price of each item.
- The total cost of the shopping.
So, we can use two variables called price and total in our algorithm.

More about variables

Variables are a very useful programming tool. Here's how variables work.
- A program can have more than one variable, but each one will have its own name.
- A variable can only work with one piece of changing information at a time.
- Variables can contain words, numbers and valueThe specific information stored in a variable at any one time. like TRUE or FALSE.
- Once information is collected in a variable, it can be used somewhere else in the program.

We can find out more by looking at the program code for a simple computer quiz game.
Welcome to the dinosaur maths quiz!
When you start the game by pressing the space bar, the dino quizbot asks for your name and shows you an answer box.
Then, once you type in your name, the quizbot says "hello" and uses your name. It's been programmed to be very friendly!
The player's name will change every time someone new starts the quiz.
Do you think we can use a variable in the program for the quiz?

Using text in a variable

A variable can use text as well as numbers.
In our quiz program, we can create and use a variable called PLAYER NAME to store the player's name once they have typed it in.

What will the code look like?

The first line of code in our program tells the game to wait for the dinosaur to be clicked, before starting the game.


The next line of code instructs the game to type "What's your name?" on the screen and wait for the player to type in an answer.


The next line sets the value of the PLAYER NAME variable to the answer from the question.


The last line of code tells the program to type the information stored in the PLAYER NAME variable, after the word "Hello" on the screen.

Using the PLAYER NAME variable like this, means the quiz can personalise the greeting every time a new player starts and the name changes.
Remembering your details online

On the internet, variables like this are an important part of small programs called cookies.
Cookies are used to remember your name and other personal details, when you log into online accounts or visit websites.
You may have seen messages onscreen asking if you want to allow these variable values to be saved.

Keeping the score
We can use a variable in our dinosaur maths quiz program, to keep track of the correct answers.
The program has a variable called score.
Each time a player gets an answer correct the program changes the value in the score variable by adding one.
Look at the code and see if you can work out what the other parts of the program are doing?

Winning the game

We can use variables with other programming tools like selection. Selection is when a program asks a question or makes a choice about what to do next.
We can use a variable to help the program decide what to do.
Here the program uses the value of the score variable to decide if the player has won the game. The player can only win if the score has reached 10 points.
Watch: Variables
You can find out more about variables in this video from BBC Teach.
NARRATION: You know the kind of day where first it rains, then the sun comes out, then it gets freezing cold? The weather forecast would say that the day’s weather is going to be variable. It means changeable - likely to change or vary. We can also use the word variable in computer programming when we need to measure or count something that changes, like a score, or a name, or the answer to a question, or a number. Sounds complicated? Let’s have a closer look…
Imagine you are playing a quiz. Each time you get a question correct you get a point. Yay! The trouble is, it’s hard to remember how many points you have as the quiz goes on. One solution might be to have a box and each time you get a question right you put a counter in the box. At the end of the quiz you could see how many counters there were in the box to find out your score. A computer variable acts like one of those boxes - it can store the changing information so that the rest of the program can use it.
So what would this quiz look like on a computer? As well as all the questions and answers, our quiz program would need a way to keep track of the number of right answers for each player. This is where our friend the variable score box comes in. We can give it a name to make it easier to use - let’s go with 'score'. If we break down our game program into the steps of an algorithm they might look like this: When the game starts set the quiz score to zero. When the player gets an answer correct change the score by adding one. So pretty straight forward so far, hey.
Now we have the points total stored in our variable box, we can use it for other things. Let’s say we want the player to win the game when they score 10 points. We can add these instructions to our algorithm. When the total points in the score variable equals 10, play the winner sound.
Oh come on, you should have got that one!
Let’s personalise our game. It would be fun if our quiz said hi to each player personally. The trouble is the player name will change, or vary. Yes, you guessed it, it’s time for another variable. A variable box can store words as well as numbers, and it can also get information from outside as well as from inside the program. First the program will need to ask the player a question: “What’s your name?” Once the player types in their name the program will store the information in a variable box called 'answer'. The program will then send instructions to show on screen. “Hi there, answer, are you ready for your quiz?”
Whatever is stored in the answer variable will go into the greeting. That way the same instructions can be used for any player, whatever their name might be. Clever eh?
Out in the real world, things are changing all the time. Names, numbers, scores, dates, measurements - you name it and they will probably be changing before your very eyes. This means variables are an important part of nearly all the computer programs and systems that we use every day. A good example that you might be familiar with is the dinner register at school. This information changes every day. Each morning the class teacher will count the number of pupils who are having school dinner and how many are having packed lunch. The total is then sent down to the school office, and used to tell the kitchen how many dinners and packed lunches to make for that class on that date.
Lots of schools now have computerised register systems, and the programs that control them will use variables to store all the changing information each day. The class name, the date, number of school dinners, number of packed lunches.
I don’t know about you, but I don’t think variables aren't as complicated as they first sounded, and they are a really important part of writing useful computer programs. And of course, for making sure you don’t go hungry at lunchtime!
Activities
SATs preparation resources. activitySATs preparation resources
Get ready for the SATs papers with videos, activities, quizzes and games to refresh your knowledge and practise your skills.

More on Computer science
Find out more by working through a topic
- count19 of 24
- count20 of 24
- count21 of 24
- count22 of 24