The previous article gave a gentle introduction to creating programs, by explaining what they are, and how to go about writing them, using a set of basic principles, which can be read here. In this article, we will look at the first concept mentioned, the variable. Hopefully by the end of this article you will know all about what they are, and why we use them.
Before looking at variables we need to know a few things about memory. Memory is what we use in computers to store data, such as audio, and images, as well as programs. There are two main types: main memory, and secondary memory. Secondary memory such as hard drives (HDDs) and solid state drives (SSDs) are used to permanently store data when our computer is not switched on.
On the other hand, main memory is used to store the data and programs that we are currently using. When we open a program on our computer such as a web browser, this program will be stored in main memory. The specifics are not important, but I will say it’s done because main memory is a lot faster than secondary memory. Either way, main memory is divided into cells whereby each cell has its own unique address. These addresses are used to locate the cell, and get access to whatever is stored in it.
When we write programs, we will often need to store data in memory, and to do this we would need to know the address of cells which are not currently holding any data, and then proceed to use that address to store the type of data we want. An address can be a large value in the range of 0 to 4,294,967,295, which is a lot of potential locations. Imagine having to remember every address we have our data stored in, and if we had to access it frequently it would be rather boring to keep writing these large values out over and over again.
Fortunately, we are lucky in that the operating system, the program that tells the hardware in our computer what to do, works out what locations are free, and stores the data for us. All we need to do is tell the computer we want to store something and provide a name that we use as an identifier to access a unique location. The operating system makes an association between an address and the name we provide. The name is then used by us, and the operating system knows which address to look for.
After telling the computer we want to store the data, and giving it a name we then need to state what type of data it is we intend to store in that location.
Again though, before going any further it seems we need to take a slight detour and look at how data is stored in a computer. When it comes down to it computers only understand what is known as binary, 0s and 1s, and nothing else. Binary numbers are a sequence of 0s and 1s, for example, 010010, 01, 0, 0100111010, etc. I know in the previous article I said that computers understand programming languages, and well in a way they kind of do. They can take instructions given in these languages, and convert them down into binary, so that they can be executed, so I only half lied!
Instructions and any type of data: images, text, words, numbers, etc, are all stored in memory as binary. By writing out the type of data we want to store we are telling the computer how to treat the binary numbers stored at that location. Taking another example 010, what does this even mean? Not a whole lot, unless we give meaning to it.
We could give this sequence any meaning, for instance, we may decide that the digit furthest to the left represents red, the second green, and the third blue. Therefore, if we had 000, this could represent black (no colour), 111 white (using all the colours), 100 red, 010 green, and 001 blue. However, we could treat these numbers any way we want, the important thing here is that binary numbers represent all the data stored in our computer.
Therefore, when we declare the type for our variable we are telling the computer what data we want to store at a specific location, and then when we go to use that data the program knows what to do with it. A variable can then be described as
A named memory location for which we want to store a specific type of data
The location of where this data is being stored is within main memory, and this means the data we want to store won’t be stored permanently. Variables are intended to store temporary data needed by the instructions in our programs.
This may seem confusing, why would we want to store data that we are only going to lose when the computer is turned off? Well when writing our instructions for a program we will want to store data which our instructions will use. All programs will have input, this could be listening to a user pressing a key, typing in text, or moving a mouse, and then we want the program to respond to us in some way, provide an output. The input passed to our program needs to be stored somewhere so other instructions can make use of it.
Additionally, the instructions we write might require the storing of data. For instance, if we want to add two numbers up we could then store the result of this in a variable. We could then use another instruction to take that variable and output it to our screen so we can see the result, think of your calculator. This means that variables allow us to temporarily store data so that we can use the data at a later date in our program, but once we close our program, the data in those variables is lost.
Finally, I think it’s worth mentioning that variables are not fixed. What I mean by this is that we are able to change the data being stored at that location. If we had the variable called number we could write an instruction that adds two numbers together, 5 + 2, storing the result of this in number, the value stored being 7. Later in our program we could then add up two new numbers 6 + 8, and then store the result in the same variable, same memory location, which will overwrite the data already stored there. If we want to keep the previous value, then we could just create a new variable, say number2, and store our result of 6 + 8 in there instead. Note you should be using better variables names than the ones I have mentioned!
To sum up then, a variable is intended to be a temporary place to store data, which is stored in main memory at a unique address. We access that data by giving our variable a name, which the operating system then uses to retrieve data from that location. Variables are used alongside instructions in the programs we write to allow us to keep hold of data input by a user, or to store data we want to use at other points in our program, by other instructions. The data stored in these variables can be overwritten, and if we want to store more data we can simply create more variables.
Sometimes when we want to store a lot of values it’s better to store them within a data structure, as opposed to creating more variables. The next article will explore the use of data structures, how they are similar, yet different from variables, and look briefly at the different types of data structures.