Illustration of money_box variable

What is a Variable in programming and how can you use them?

How is a Variable Defined?

Let us start off by checking out what Wikipedia has to say about Variables:

In computer programming, a variable or scalar is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value; this separation of name and content allows the name to be used independently of the exact information it represents. The identifier in computer source code can be bound to a value during run time, and the value of the variable may thus change during the course of program execution.

I’m confused, what exactly is a variable to a human like me!?

Variables are a core component of all programming languages and serve the basic and important function of storing information. Let’s explore what they are as I attempt to explain all about them with some good depth. When trying to understand what a variable is, I find using a box or an envelope to be a good analogy. For instance, you might store money in a box. You throw $20 into a box and write “money_box” on it. Your variable is then “money_box” with a value of $20. Now what is “$20” when you start thinking with programming? It is a number, and more accurately, an Integer, a whole number. Knowing the type of data you’ll be storing in a variable is pretty important to keep in mind so that you use them correctly.

Illustration of money_box variable

So you now understand that “money_box” represents $20, great. If we come up with a simple problem to solve with programming we can see some use for that. For now, pretend you are trying to save up for a new car. Your current money is known as “money_box” and your goal variable is “money_goal” with a value of “3000”. Now what happens when we get paid our check of $1337? Where do we stand compared to our goal? Let’s check with a simple php program.

As you can see, using variables we can perform basic operates such as addition, subtraction and division to calculate values. We also can update an existing variable with a new value so we (the programmers) don’t have to keep track of it. Now as I mentioned, you can do math using variables (or just values) when working in code. What might happen if you tried to add the text “bacon” to the value of “money_box”, like so: ?

Well, you will either get a crash-n-burning error in a strict language, or you will end up with “money_box” having a value of “20bacon”, or something to that effect. It is probably not what we desired to have happen. Variables have a type of data, and that makes sense. We can’t treat “20 + bacon” as math in the real world without treating “bacon” itself as a number. This is why knowing what data types are is important, so we can avoid trouble that can come around with different operations.

What kind of data types are there?

The most common data types you will encounter are BooleanString, Integer, Float, DoubleArray and Object.

Boolean is the most simple. Its value is either True or False. Nothing else, ever.

A String represents text.

Integer is a whole number.

Float is a number that may reach 2 to 6 (usually) decimal places.

Double is a number that has much more decimal depth than floats do.

Arrays can be thought of as a box (variable) holding more boxes of any data type.

Objects are similar to arrays as they also hold other variables. However, objects have considerably more complexity here and going forward.

Why are data types important?

So you now have a general idea of what data types are in programming. Why they are so important to keep track of? When you are build a program you are going to have different functions working with data. Even a fairly simple program is going to expect input in a certain manner, and it will fail if it is given incorrect input. It will be helpful to keep this in mind when looking for errors in your code as your progress in your learning. After enough time and experience it is one of those things that will become (mostly) second nature.

Add your comment

Your email address will not be published.