What is a Variable?

I mean it varies. But what else? What is a constant?

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

Variable: A named value, that potentially can be changed as the program runs

Variable name: A label for a memory location

Data: Information processed or stored by a computer

Storage: A place where data can be stored

Program: A computer program is a set of instructions to perform a task on a computer

Reference: A pointer to a variable or object

Declaration: Announcing a variable (usually) at the beginning of a program

Naming convention: A set of rules about the names of variables

Value: The something that would be stored in a variable

Assignment: Giving (setting) a variable a value

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
Image for post
Image for post
Putting an Integer (4) into a box myNum

This is particularly useful for taking user input and performing some operation on that, perhaps multiply it by two.

Image for post
Image for post
The process of taking an input number and multiply be 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.

Image for post
Image for post
var myNum = 4
myNum = 5

Storing different data types

We can use variables to store all sorts of different types of data.

Image for post
Image for post

We might be limited by our programming language, but as shown above 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”.

Image for post
Image for post

The difference in this particular box is that it can’t be opened again. That is reassignment is not possible).

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.

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.

Want to get in contact? Try the link here:

https://twitter.com/stevenpcurtis

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store