What is a Variable?

Back to Basics

Simply put, a variable is a representation of a value that can change.

One of the most important elements of software is a variable. Anyone familiar with math variables already has an understanding of what a software variable is but there are some details to be aware of when using variables in software.

Let’s start with a simple example, the math function A = l * w, A represents the area of a rectangle, l represents the length and w represents the width. In math, variables are typically one character and this is in part because they are generally the same for most situations (l = length, w = width, h = height, etc). Sometimes a key is used in cases where the meaning of the variable may not be clear or the variable typically represents one thing but in your function it represents something else.

In software, variables are rarely one character, typically they are a whole word or even multiple words in some cases. A variable has the same meaning in software in the sense that it represents some value and despite what that value is, the function remains the same. Going back to our math function for the area of a rectangle, in software we might use more meaningful variable names like the following.

area = length * width

In math, many functions are known in our heads so it’s easy to intuitively understand what A, l and w represent but in software it’s not as clear because variables aren’t always used in math functions so we take advantage of using clear and concise variable names to allow ourselves and others to understand what the variable is meant to represent.

Variable Declaration

Variables can be declared in quite a few various ways.

Despite the many ways to declare a variable, every variable declaration has the same breakdown which we’ll go over now.

Variable Declaration Breakdown

I will provide more detail for some of these attributes in later posts but for now I’ll cover each of them briefly.

Modifier

The const modifier is reserved for variables intended to be constant, that is, variables with a value that is not going to change. This may sound like a redundancy and it is but remember that a variable is simply a way to store information and sometimes that information won’t be changing. An example of a constant variable is one like pi, the value of pi will never change and most math libraries will include a constant referred to as PI.

Data Type

Data type is a topic that will require it’s own post entirely, I’ll be going into this in more detail later. For now, know that the data type is one of the few attributes that is always required in a variable declaration.

Informative Name

I specified informative because this is a very important thing to keep in mind. Code should always be well documented and as one of my favorite books The Pragmatic Programmer states, variables should be thought of as documentation therefore names should be well chosen and meaningful. For example, despite being engineers and knowing that dt typically represents delta time, the extra time should be taken to write out a variable name like deltaTime, you will find it much easier to understand as the program gets more complex or when revisiting code after stepping away for a significant amount of time. Informative names also help others who are reading your code better understand the purpose of the variable.

Assignment

Variables require, at the very least to be declared, variable declaration is simply telling the compiler to reserve a space in memory. Following declaration, sometimes during declaration, is variable initialization or assignment. Variable initialization is when you assign a value, this can be done during declaration or at a later time. Be forewarned, with Arduino, if you don’t assign a variable’s value immediately the value will definitely be something but most likely garbage left from whatever used that storage location previously. Some languages automatically assign new variables to zero or null but C++ is not one of those languages. When you declare a variable, the space is reserved but not cleared so it will contain whatever data was there from some previous use of that same location. It’s typically good practice to assign a value immediately, even if simply assigning it a value of 0, at least this way you know that it’s a valid number and zero can be used to indicate that it’s brand new.

The only difference to be aware of when assigning values is when storing information that isn’t numeric, one example being strings. Strings store characters which can be numbers, letters or both but need to be enclosed with quotes when assigned as follows.

Note again that if you don’t immediately assign your variable with a value then there is no telling what the value will be until it is assigned. A good habit to get into would be to assign numbers with 0 and alphanumeric variables with “” to ensure that you know what the value is starting out.

Parting Caveats

Variable names must start with a letter or underscore, if not, the compiler will fail to compile.

C++ is case sensitive and therefore a variable named value is different than one named VALUE or even Value for that matter. The important thing to note here is that while these are all allowable names for representing different variables it would be frowned upon to use case sensitivity as a differentiation of two or more variables as it would introduce confusion.

Another important aspect of naming is to keep the names specific and clear as to what the variable represents. If you have a few variables holding a value for a delay time, rather than using names like delay1delay2, etc try to add more information to the name, maybe something like ledPulseDelay and sensorReadDelay.

Leave a Reply

Your email address will not be published.