Variables

What are variables?

A variable is a simple way of storing one piece of information somewhere in the computer’s memory whilst a program is running, and of getting that information back later. A variable is an example of a data structure. As pupils move on to Secondary school, they will learn about other data structures such as arrays. A variable can be numerical, textual or perhaps an indicator of true/false. Programs can store, retrieve or change the values of variables. Variables are an abstraction of data storage and retrieval: as programmers, we need not know how exactly the programming language, operating system and hardware manage and handle them.

A screenshot of a variety of different variables.

Examples of variables: username, password, each detail of an online order, and scores and lives in a game.

One popular way to think about a variable is to imagine that it’s like a box that can hold values. The box is labelled; this is the name of the variable. The variable can only store one thing at a time. If we retrieve the value from a variable, we look to see what that value is but do not remove it: nothing has been taken out of the box. We can update what is stored in the variable at any time as the program is running – for example, increasing the value of a score variable in a game.

An image of a box representing the variable "Score".

A variable can be thought of as a box that can hold one value at a time.

The process of creating variables is sometimes called declaring. In Scratch, to create a new variable, you simply give the variable a name and decide if it will exist for all sprites or just one sprite. We need to remember to set a value for each variable at the start of our program; this is called initialising. If we do not remember to do this, and then re-run the code (in the same session), the variable retains the value that it had when the program was last run. This could mean we find ourselves playing a game starting with the previous player’s score!

A screenshot of examples of creating variables in Scratch, Python and MSW Logo.

Examples of creating variables: in Scratch, in Python and in MSW Logo.

Why are variables important?

Variables and other data structures are how computers store and manipulate information. Variables capture entered data; they enable values to be displayed and fed through to other systems; they hold intermediate values in calculations. We teach pupils to work out which variables are present in systems and programs, drawing on the same range of computational-thinking principles used by ‘real world’ developers. The use of meaningful, unambiguous names for variables facilitates the reading of code, especially important in large-scale programming projects where multiple teams may work on code over months, if not years; poorly named variables obscure the purpose of code and can make debugging almost impossible. Certain staff may even have the specific role of maintaining consistency and coherence in the names of variables and other data structures, often using standardised naming formats.

What do variables look like in the Primary curriculum?

EYFS and Key Stage 1 provide a great opportunity for children to recognise how variables are used in the world around them, although they don’t start using them within programming until later in the Primary phase. Exactly when this happens depends on the language they use. As they progress through Key Stage 2, it may be useful to first have them identify where variables are being used in existing programs, before they start using variables in their own coding. This is something akin to reading before writing. Likewise, unplugged activities can be used to develop pupils’ conceptual understanding of variables before applying this in a programming context. Towards the end of Key Stage 2, children use variables in a more complex manner; for example, to control repetition and the flow of programs through conditions in selection. They also learn, as they develop their plan and algorithm, to identify which variables a program might require.

The concept of a variable is one with which many pupils struggle and it’s worth showing them lots of examples to ensure they grasp it. In Secondary school, they’ll go on to learn more about how data is stored. They’ll find out about handling groups of associated variables using programming structures such as lists, arrays, tables and databases. They’ll also learn more about the scope of variables, including local and global variables.

3 - 5 years

Early Years pupils aren’t expected to use variables in any programming activities. However, they’re starting to use general terms to describe things, and they understand that things change over time or differ between people. We might talk about the general term “weather” and say that it’s raining today. In this scenario, the variable is Weather and its current value is Raining. When children learn the names of their classmates, they learn that each has their own name; so, this variable is called Name and the value for their best friend is, for example, Charlie. As pupils use generic terms and record values for them, they’re starting to learn about the underlying concepts associated with variables.