Language: EN

programacion-que-es-una-variable

What is a variable

A variable is a designated container for storing a data or a value, which we give a name to access, manipulate, and reuse throughout the execution of the program.

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

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

variable-edad

Your age variable

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 our box a name, so we can access it, find it, and differentiate it from the others.

Now I take my box “age”, and inside I place the number 25. And we have another property of variables, which is that they are used 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

Your age variable with 25 inside

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

variable-edad-26

Your age variable, now with 26 inside

So a year later, when the person has a birthday, we can open the box “age”, change the number to 26, and close it again. Now, when we ask the box “age”, 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 a bit broad to say that a variable varies, but that’s how it is (in contrast, those that cannot vary are called constants).

The value we can store can be anything allowed by the language. From a number to a string of text, or a more complex object like 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 accessed during runtime.

For example, a code

myText = "an impressive text"
myNumber = 42

Here,

  • myText is a variable that stores the text “an impressive text”
  • myNumber is a variable that stores the number 42

Depending on the language we are using, we will have access to some types of variables or others. On the other hand, there are languages with strict typing or languages with dynamic typing.

Declaration of a variable

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

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

For example, in C# (strict typing) to create a text variable we would do.

string myText = "an impressive text";

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

let myText = "an impressive text";

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

myText = "an impressive text"

Assignment of a variable

To change the content of a variable throughout the program, we simply need to assign it a new value. There is consensus on this, 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 impressive text";

Which would be identical to the case of JavaScript

myText = "another even more impressive text";

And even to that of Python,

myText = "another even more impressive text";

However, it should be noted that if, for example, we make a mistake in writing the name of the variable and put myText, instead of myText

myText = "another even more impressive 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 it would create a new variable called myText, without any kind of warning.

As we can see, allowing variables not to be declared can be both an advantage, as it is very convenient, and a possible source of errors.

Assignment of 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 incrementing the counter by one.

This is because the evaluation of that expression is evaluated 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 will not do a “I my grandfather am”, nor crash your computer or anything. It simply first evaluates what is on the right, and then stores it in the variable on the left.