# Fortran Tutorial Lesson 2 | Variables and data types

Variable and Data Types topic is the must read topic to start learning any programming language. We will first understand the concepts of variables and data types, before going to code.

Make sure you learned the Lesson-1 , before reading this.

## What is Variable?

Variable, in any programming language, is like a store box with fixed name to store certain value. It’s like a variable in algebra, where we define unknown number by some variable. And, the value can be calculated for that variable.

lets say, we want a program to print out the age of some people. We have to create a variable(box), to store the value of age.

In FORTRAN, we should know the type of value to be stored, before creating the variable. Therefore, we should be aware of data before creating variable.

## Data Type in FORTRAN

In FORTRAN, we have 5 different types of value (data) that can be stored in variable: They are:

• integer
• real
• complex
• character
• logical

All these data types help us to ensure the availability of data for the program.

### What are those data?

• integer data type means the numeric value without decimal, like 4, 100, 39
• real data type means the numeric value with decimal, like 4.0, 5.78, -45.23
• complex data type means value with real and imaginary part, like (3, 4), (3, -5)
• logical data type means boolean value. They are True and False.
• character data type means text or characters like “hello”, ‘a’

## Code

As I told before, we should declare the variable first, to store the value. Our declaration of variable code goes like this:

``data type :: variable name``

for eg.

``````program ex
integer :: age
end program``````

In the above example, we created a `age` variable, to store only integer value. So, lets store integer value there.

``````program ex
integer :: age
age = 47
end program``````

For this program to work, we have to add one keyword “`implicit none`” before the variable declaration.

### Implicit none

In the old version of FORTRAN, people mostly used 1 character for a variable name like `i, a, x `etc. So FORTRAN used to declare data type to the variable automatically — i, j, k, l, m, n such characters were declared as integer and real for the rest.

But we don’t want that in our program. So Modern FORTRAN allows us to choose variable name of our choice and declare them as our choice.

So to tell our program, that our declarations are right, we have to put implicit none before declaration. Hence:

``````program ex
implicit none
integer :: age
age = 47
end program``````

We stored 47 value in the age variable, now we can print this value using `print*, `statement:

``````program ex
implicit none

integer :: age

age = 47
print*, age
end program``````

The output of above code comes: `47`

Now, create different variable for different data types, and play with code yourself. But you should know that, all variable names are not accepted. There is certain rules to write variable name.

## Line Indentation

FORTRAN programming language is very flexible to write. There is no need of any indentation in any piece of code.

We just have to write our code between the `program` and `end program`. We can give any spaces we want. We can also put many blank lines in the code. for.eg.

``````program ex
implicit none

integer :: age

age = 47
print*, age
end program``````

It still is the same code and gives the same output without any error.

However, there is an art of writing program code. So I suggest you give an indentation of your choice to make it more readable. for e.g:

``````program ex
implicit none

integer :: age

age = 47
print*, age
end program``````

## How to write Variable name?

• variable name cannot be start with letters like 1, 2, 9 or special characters like \$, % etc.
• but you can insert number after using letter. e.g.` name1, how2do` etc.
• There should be no space in variable name. Instead of `my age` variable name, we can say, `my_age` or `myAge`.
• we cannot use these reserved words as variable name.
• We have to declare variable data type before storing value in it.

Learn and practice more.

Watch video on variable names and data types for more understanding.