Skip main navigation

What are variables?

Variables can store data that can vary, change, or can be changed while your program is running. Read more about them here.

Sometimes your program will need to take into account things that are different each time the program runs, or even change while the program runs. In this step, I’ll explain how you can use the programming concept of ‘variables’ to deal with this.

Variables in the real world

In the real world, something can be described as variable if it varies, changes, or can be changed.

Something that varies: People’s names are an example of something that varies, e.g. my name is likely to be different to your name.

Animation of a person saying hello to several different people. Each time they say "Hello …" which doesn't change, but a new name appears for each new person.

Something that changes: The weather is an example of something that changes, e.g. today it is sunny, yesterday it rained.

An animation of a woman looking at the weather through a window. The weather changes between the sun in a clear sky, the sun partially hidden behind a cloud, and a raining cloud. While the weather is sunny the woman is happy, but she is sad when it is raining.

Something that can be changed: The scoreboard in a football match can be changed to reflect the current score.

A football scoreboard reading Liverpool 0 - 0 Manchester, then changing to Liverpool 0 - 1 Manchester.

All of these examples can be described as variable, but in different ways and for different reasons. Different people have different names, but those names don’t change often or at all. The weather changes continually and can change over a few hours or even less. Scores in a football match change only when a particular event occurs: each time someone scores a goal. Computer programs also use variables in different ways, similar to these.

Variables in computer programs

Here are some examples of how ‘variables’ are used in computers programs.

A program may ask a user to enter their name. This value is then stored as a variable. The program can then use that name to personalise the experience. For example, instead of asking “How are you feeling?” the program could ask “Hi John, how are you feeling?” When someone else uses the program, the variable value can be set to the new user’s name.

A reading can be taken using a temperature sensor and temporarily stored by the program as a variable value. This variable value can then be displayed on a device and/or used to control a system, such as a heating system.

Variable values are also used in games. They can have a variety of uses, for example, to store scores, lives, time elapsed, or time remaining.

These three examples mirror the real-life examples above. The name varies, the temperature changes, and the player’s score in a game can be changed. It can be useful for you to think about variables in both real-life and programming contexts to help link the abstract idea of a variable to more concrete examples.

Task

  • Think of three real-life variables
    • Are they things that vary, that change, or that can be changed?
    • Is it possible for one variable to fit several of these categories?
  • Reflect on programs or apps that you have used recently
    • Where have you seen variable values used?
    • What causes those values to change?

Share your thoughts in the comments section.

This article is from the free online

Introduction to Programming with Scratch

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