Language: EN

programacion-que-es-una-variable

What is a variable

A variable is a container designated for storing data or a value, to which we give a name in order to be able to access, manipulate, and reuse throughout the program’s execution.

Our program will have to manage data. In fact, that is its main function, to manipulate and manage data. And, in some place we are going to have to store those data. That place is the variables.

It is usually used to say that a variable is “like a box” where we store data. It is quite suitable, and it is useful to explain the concept, so here comes this box!

variable-edad

For example, we can have a variable called “age” that stores a person’s age. The first thing we see is that we have given a name to our box, in order to be able to access, find it, and differentiate it from the others.

Now I take my “age” box, and I place the number 25 inside. And we have another property of the variables, which is that they serve to store things. Variables can contain different types of information, such as numbers, names, colors, or anything we need in our program.

If at some point I want to check the age I have stored, I simply open the box, and get the number 25.

variable-edad-25

The advantage of variables is that their content can change. But it is not necessary to change the box, our “age” box remains the same. We are simply going to change what it contains.

variable-edad-26

So in a year, when the person has a birthday, we can open the “age” box, change the number to 26, and close it again. Now, when we ask the “age” box, it will give us the updated number 26.

In summary:

  • Variables contain things (information)
  • They have a name to differentiate them from others
  • The content can change, without needing to change the variable itself

Content of a variable

A variable is called that because its content can vary. I know, it’s very broad to say that a variable varies, but that’s how it is. Conversely, those that cannot vary are called constants.

As for the value that we can store, it can be anything allowed by the language. From a number, to a text string, or a more complex object such as the complete list of orders from a factory.

The variable has a name and an associated data type, which allows it to be identified and its content accessed at runtime.

For example, a code

myText = "an awesome text"
myNumber = 42

Here, myText is a variable that stores the text “an awesome text”, and myNumber is a variable that stores the number 42.

Depending on the language we are using, we will have different types of variables available. On the other hand, there are languages with strict typing and languages with dynamic typing.

Declaring a variable

In many programming languages, a variable must be declared before using it. This means telling the compiler or interpreter “hey, I want to create this container with this name!”

Additionally, in typed languages we must specify the type of data that the variable can store.

For example, in C# (strictly typed) to create a text variable we should do.

string myText = "an awesome text"

In JavaScript (dynamically typed) we need to declare the variable, but it is not necessary to indicate its type. It would be like this.

let myText = "an awesome text"

On the other hand, in Python it is not necessary to declare the variable or indicate its type. So the syntax would be like this,

myText = "an awesome text"

Assigning a variable

To change the content of a variable throughout the program, we simply have to assign a new value to it. Here there is a consensus, and in almost all languages the ’=’ operator is used.

Therefore, when the variable already exists, in C# to change its value we would do.

myText = "another even more awesome text"

Which would be identical to the case of JavaScript

myText = "another even more awesome text"

And even to that of Python,

myText = "another even more awesome text"

However, it is worth mentioning that if, for example, we make a mistake when writing the variable name and put myText instead of myText

myText = "another even more awesome text"

In C#, JavaScript, and in most languages, it would give us an error because that variable does not exist (we have not declared it). However, in Python a new variable called myText would be created without any notice.

As we can see, requiring to declare variables can be both an advantage, because it is very convenient, and a possible source of errors.

Assigning a variable to itself

During the assignment of the variable, it is possible to use its own content to calculate the new value. For example, it is valid and very common to do the following,

counter = counter + 1

With that statement, we are increasing the counter by the value of one.

This is because the evaluation of that expression is done in two steps,

  • The expression on the right is evaluated
  • The resulting value is assigned to the variable on the left.

There is no problem in having the expression on the right contain the variable itself. It is not going to do an “I am my own grandfather”, or crash your computer or anything. Simply the right is evaluated first, and then it is saved in the variable on the left.