What is a Variable?
Variables are vital for us in any modern programming language. But what exactly is a variable, and can we make an analogy for people reading this article? I say yes, on both counts.
Difficulty: Beginner| Easy| Normal | Challenging
Prerequisites:
- None, but for practical purposes it would be useful to be able to produce a “Hello, World!” application in your chosen programming language
Terminology
Assignment: Giving (setting) a variable a value
Constant: A variable that cannot be changed
Data: Information processed or stored by a computer
Declaration: Announcing a variable (usually) at the beginning of a program
Naming convention: A set of rules about the names of variables
Program: A computer program is a set of instructions to perform a task on a computer
Reference: A pointer to a variable or object
Storage: A place where data can be stored
Value: The something that would be stored in a variable
Variable: A named value, that potentially can be changed as the program runs
Variable name: A label for a memory location
What are variables?
You might have produced a “Hello, World!” program in your chosen programming language. It is all very well, but limited.
More complex programs need a way of labelling data with a name so it can be referenced later. This has the rather wonderful side-effect of making computer code easier to read (as we can now will our computer code with helpful-sounding names).
We think of variables as containers that hold information and allow us access them later. We will think of this as a box that has a label on it. The label is very useful because it means that we can refer to the box later.
We can visualise this a box that has a value added to it. In the image below we add 4 to myNum, this represents
var myNum = 4
This is particularly useful for taking user input and performing some operation on that, perhaps multiply it by two.
We can move this on with reassigning variables.
Changing variables after initialisation (reassignment)
You can use the same variable (box) several times. However, if you rewrite a variable you will lose the initial value stored.
var myNum = 4
myNum = 5
Storing different data types
We can use variables to store all sorts of different types of data.
Integers, Floats, Characters and Strings are certainly options to be stored in a variable.
Storing constants
What if we place 4 into a box, and permanently seal the box so it cannot be changed in the execution of our program?
Well, this would be called a constant.
let myNum = 4
const myNum = 4
constant myNum = 4
In declaring a constant, depending on the language we may call it “let” or “const” or even “constant”.
The difference in this particular box is that it can’t be opened again. That is reassignment is not possible in this particular case).
Technical note
The explanation involved with boxes roughly relates to the concept of pass by value rather than pass by reference. Interested in these terms? You can read more about this over on another of my Medium posts, although the explanation is quite in-depth!
The video
Conclusion…
Variables are vital in any programming language as they allow us to write flexible programs. Rather than directly entering data we can represent the data with variables which is then swapped for the real data when the program is run.