If you’ve had your interest piqued in coding in recent weeks or months, you may be keen to learn more but feel a little overwhelmed when it comes to some of the more technical language.
One term that crops up a lot is “variable”. Here we’ll explain the basics of what variables are, how they work, and why we use them.
What Is A Variable?
A variable is a storage location which contains information, commonly referred to as a value. Variables can be manipulated by programmers and are given symbolic names (known as identifiers) to help users recognise them. Usually, this name will reference the information that’s stored in the variable, although it can be built so that this changes during the run time.
The easiest way to understand what variables are is to imagine them as the office drawers that contain the information you need. They don’t represent the information itself, but they hold the info and their structure can also influence how it is stored.
How To Name Variables
Naming variables in this way is very useful, as it allows them to be used in a whole host of programmes rather than just your own. However, naming variables (known as “assigning value”) is notoriously difficult, as what makes sense to you may seem unusual to somebody else. In some cases, a value you’ve given to a variable yourself may be hard to recognise if you’re revisiting months or even years later.
To help with this process, variables are typically assigned values that work as simple descriptors of the information they’re holding. If a variable is storing data on costs, you would call the variable “COST”. In certain coding languages, the nature of the syntax of the language itself. However, even this factor is relatively easy to overcome once you get acquainted with the syntax.
Another factor to consider is that some older languages are not case-sensitive, meaning that a variable with the value “COST” would be different from another with the value “cost”. To avoid any pitfalls, most programmers will use a set of style guidelines that advise on how to assign values to variables.
What Are The Different Types Of Variables?
There are several different types of variables, and they are classified by how their name defines their lifetime. The types of variables are:
- Global variable
- Class variable
- Instance variable
- Local variable
Constants store data which never needs to change (e.g. the number of days in a week), and therefore not really “variables” at all. Typically, this is communicated by capitalising the value of the constant.
Global variables are not bound in terms of their lifetime as other variables can be. Also known as a static variable, global variables cannot be changed until the memory cell is terminated.
Also known as static variables, class variables occur only once within a class, irrespective of how many objects are created from the variable.
Instance variables can cross some scope boundaries but not others, meaning that they serve as a midpoint between class variables and local variables.
The most common type of variable, local variables function within all scope boundaries.
Variables are the type of function that can seem quite confusing to a reasonably inexperienced coder, but with a little persistence and study, you’ll be using them with no trouble at all.