Variable: what is it?

Learn the meaning of a programing variable. Understand how programming languages use them to store and process information.

Before learning about variables, it is recommended that you have already read our article on input and output commands. It is your starting point in computer programming!

But let’s go!

If you read any technical book on programming language, you will certainly have a very superficial definition of what a variable is. Something like: “a variable is a reference to a computer’s memory address”.

Just to illustrate, see below an example of a RAM memory device, which is the computer’s memory. Note that it is made up of several rectangular black chips.

In fact, inside each of these black chips there are spaces where information is stored electronically. These spaces are the memory addresses.

So, in a superficial way, we can say that a variable is a reference to those spaces inside the computer’s memory chips.

What is a variable for?

In order to understand very easily the usefulness of a variable, look at the example below:

Imagine that I ask you to do a simple multiplication calculation. If I asked you to calculate 3 x 5, you would undeniably tell me the answer right away, wouldn’t you? It certainly does not take much mental effort to make this calculation. The information fits all in your brain.

variavel-variable-01a

Now, if I asked you to calculate 342.56 x 53.17, things would certainly be different, wouldn’t they? You would definitely need more time, effort and a sheet of paper and a pen.

variavel-variable-02a

It turns out that this last calculation has much more information, in such a way that that information does not fit all in your memory. You need to record everything on a sheet of paper.

you write the numbers on paper…

variavel-variable-03a-01

performs part of the calculation…

variavel-variable-03b-01

write these results back on paper…

variavel-variable-03c-01

then revisits the numbers previously written…

variavel-variable-03d-01

you do some more calculations…

variavel-variable-03e-01

you write more results…

variavel-variable-03f-01
until you find the final result.
variavel-variable-03g-01

The sheet of paper works as an extension of your human memory, storing the entire amount of information and allowing you to review it during the calculation process.

It is the same with computer programs. They need to store the information they process somewhere, to revisit and reuse it. This place is the variable, which works like the computer’s sheet of paper.

Variables can be thought of as boxes. In other words, containers reserved for programs to store their information.

When the programmer writes a computer program, he uses the variables to inform where the programs will store their data during its operation.

Types

Variables are classified by types. Each type defines the characteristic and size of the information that can be stored within the variable.

For example:

  • a variable of type char is prepared to store letters and symbols, such as, ‘a’, ‘b’, ‘c’, ‘@’, ‘&’, among others, and usually occupies a space of 2 bytes.
  • a variable of type int is prepared to store whole numbers, such as, 1, 2, 3, 500 and 2756, and usually occupies a space of 4 bytes.

Below is a list of the main types of variables that exist in the main programming languages:

byte

stores integer value, but with a maximum of 1 byte of information.

short

stores integer value, but with a maximum of 2 bytes of information.

int

stores integer value, but with a maximum of 4 bytes of information.

long

stores integer value, but with a maximum of 8 bytes of information.

float

stores decimal value, but with a maximum of 4 bytes of information.

double

stores decimal value, but with a maximum of 8 bytes of information.

boolean

stores logical value of True or False.

char

stores letters and symbols, such as ‘a’, ‘b’, ‘c’, ‘@’ and ‘&’, but with a maximum of 2 bytes of information.

It is important to note that some programming languages have differences in size in bytes for some of their types of variables (such as the Python language int type, for example).

How do you create a variable?

In most programming languages, we can create a variable with the following structure:

TYPE IDENTIFIER = VALUE;

Where the identifier has the purpose of naming the variable, according to the nature of the information it will store.

For example: imagine that we need to create a variable to store an employee’s salary. Thus, the statement would be made as follows.

float salary = 3500.00;

In the same way that we created a variable for salary, we could create a variable of type character, to store the gender of an employee (where the letter ‘m’ would represent male and ‘f’ would represent female).

char gender = ‘m’;

An important fact is that some programming languages (Python and Javascript, for example) do not require a type definition when creating the variable, in such a way that its type is automatically identified by the value assigned to it. See below.

gender = ‘m’;

In this example above, the type of the variable “gender” is automatically set to the char type after inserting the value ‘m’ (in Python and Javascript).

Declaration and initialization

The creation of a variable is divided into two parts:
  1. Declaration and
  2. Initialization.

Let’s consider the model for creating a variable:

TYPE IDENTIFIER = VALUE;

The left part of the = symbol is the declaration, because it is the part where we create the variable:
TYPE IDENTIFIER = VALUE;

The right part of the = symbol is the initialization, because it is the part where we put a value inside the variable:

TYPE IDENTIFIER= VALUE;
Another important thing to comment is that, in almost all programming languages, declaration and initialization can be performed in different commands. See below:
TYPE IDENTIFIER; // this is a declaration

IDENTIFIER = VALUE; // this is a initialization

See in the code below that, in line 1, we just declare the variable gender and, in line 2, we initialize the variable gender with the value ‘f’.

1.char gender; // declaration

2.gender = 'f'; // initialization

It is important to inform that, in most programming languages, the texts that come after the // symbol are interpreted as comments, in such a way that they are not part of the code.

In Python language, comments are written with the # symbol, instead of the // symbol. See the example below.

1.gender = 'f'; # this is a comment in Python

Exercise

Now that you are able to visualize the meaning of an algorithm, you are certainly ready to solve the exercise below.

Do you want to expand your knowledge about variables further? Then, see our articles on the 5 categories of programming operators. Learn how variables can be explored using operators:

In any case, we recommend that you follow the standard learning flow and read the next article on the assignment operator.

David V. Santiago

David V. Santiago

MSc in Systems and Computing, Professor of Computer Programming, Algorithms and Data Structures.

Was this article helpful to you?

So support us and share it with others who are interested in this subject!

Share on whatsapp
WhatsApp
Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on email
Email

Follow us
on our social media.

This website uses cookies to ensure you get the best experience on our website.