Explaining variables
Have you ever heard the person who talks about the weather on TV say that the weather is going to be variableA named piece of data (often a number or text) stored in a computer鈥檚 memory, which can be accessed and changed by a computer program.?
They mean that the weather might change from one thing to another. It might be cloudy, then sunny, and then it might rain.
Variable is another word for something that changes.
Things that change
Some things, like the weather, change all the time and we don鈥檛 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.
Familiar variables
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.
Using variables in computing
We use the word variable in computing to describe a place in a computer鈥檚 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.
For example, 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.
A variable can also be used in an alarm app on a mobile phone. It can store the different time settings for the alarm, an early start on weekdays and a bit later at the weekend, when you want a lie in!
What does a variable look like in a computer game?
Let鈥檚 look at an example.
This simple computer game is called Dinosaurs and Doughnuts.
The computer programmerSomeone that creates programs for a computer. has written a list of instructions called an algorithmA precise set of ordered rules or instructions that can be followed by a human or a computer to achieve a task. to explain what should happen in the game.
Dinosaurs and doughnuts game algorithm
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?
It is the score that will change. The score is the variable.
The dinosaurs all stay in the same place, and the doughnuts don鈥檛 move or change.
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.
This is what the program codeA language that a computer can understand. 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.
But what about if the player clicks on a doughnut? Let鈥檚 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?
The correct answer is A. Did you get it right?
Activities
Computing - Dance Mat Typing. gameComputing - Dance Mat Typing
Build and test your computing skills with different levels of touch type challenges
More on Computer science
Find out more by working through a topic
- count7 of 24
- count8 of 24
- count9 of 24
- count10 of 24