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.
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.
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…
performs part of the calculation…
write these results back on paper…
then revisits the numbers previously written…
you do some more calculations…
you write more results…
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.
- 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.
stores integer value, but with a maximum of 1 byte of information.
stores integer value, but with a maximum of 2 bytes of information.
stores integer value, but with a maximum of 4 bytes of information.
stores integer value, but with a maximum of 8 bytes of information.
stores decimal value, but with a maximum of 4 bytes of information.
stores decimal value, but with a maximum of 8 bytes of information.
stores logical value of True or False.
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.
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).
Declaration and initialization
- Declaration and
Let’s consider the model for creating a 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:
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
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.