Variable: what is it?

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

11/09/2023

youtube-thumb
algol-youtube-play

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

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

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

performs part of the calculation…

variavel

write these results back on paper…

variavel

then revisits the numbers previously written…

variavel

you do some more calculations…

variavel

you write more results…

variavel

until you find the final result.

variavel

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.


Primitive Data 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.

These are the main types of variables that exist in the main programming languages. They are known as Primitive Data Types.

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

Continue with your programming learning and read the next article about the assignment operator.

autor

David Santiago

Master in Systems and Computing. Graduated in Information Systems. Professor of Programming Language, Algorithms, Data Structures and Development of Digital Games.

Outros artigos