One of the tasks of a computer program is:-
To be able to keep track of changing data.
This is the purpose of a 'variable'.
A variable is a location in memory that holds one or more values. It has a label or name to identify it and its values can be changed as the program runs.
The programmer gives each variable a name to identify it. This name is used by the program to find the memory location where the variable's data is stored, in order to read or modify it.
The simplest type of variable is a single value, for example:
The line of code above is placing the value '10' into a variable called 'z' which is located somewhere in memory.
One of the handy things about having a name to a variable is that you do not need to care where in memory 'z' is located - the operating system is taking care of all that.
If you want to use 'z' a bit later in the code, then you use its name, like this
Line 10: z = 10 // assign 10 to z Line 11: another_variable = 1 // assign 1 to another_variable Line 12: new_variable = z + another_variable // do a calculation and store in new_variable
Line 12 is using the 'z' variable as part of a calculation.
Variables can get more complex than this. For example a variable might be a list of values (called an array):
This is discussed further in a section covering arrays.
Challenge see if you can find out one extra fact on this topic that we haven't already told you
Click on this link: What is a variable?