Variables
Last updated on 2025-04-25 | Edit this page
Overview
Questions
- How do we declare and assign values to variables?
Objectives
- Understand the different intrinsic data types.
- Declare and assign variables and parameters.
Variables store information we can use in our programs. Fortran is a strongly typed language. Each variable must be declared with a type.1 Fortran provides the following intrinsic (built-in) data types for variables:
-
integer
: A whole number which is positive, negative or zero. -
real
: A real number includes the fractional part, even if the fractional part is 0. -
complex
: A number made up of a real and an imaginary part. -
logical
: A boolean value which can be .true. or .false.. -
character
: A single ASCII character. Strings are made from sequences of characters.
Variable names must be 63 characters or less (F2003 standard). No
spaces are allowed. Names can contain the characters a-z
,
A-Z
, 0-9
and the underscore _
. No
spaces are allowed. Names must begin with a letter. This also applies to
names for programs, modules, subroutines, and functions, which you will
learn about later in the course.
Do not use Fortran keywords as variable names. Fortran will let you overwrite these keywords if you’re not careful. The Fortran wiki keywords page contains a list of all Fortran keywords.
Declaring Variables
Fortran variables are declared with this syntax:
So to declare an integer:
Create a variables
program
- Create a new Fortran program named
variables.f90
. - Declare 5 variables. One of each of the 5 intrinsic data types.
- Print your variables.
- Compile and run your program.
What do you notice about the output?
FORTRAN
program variables
implicit none
integer :: number_of_pelicans
real :: pelican_weight
complex :: pelican_population_dynamics
logical :: is_young_pelican
character :: pelican_tag
print *, number_of_pelicans
print *, pelican_weight
print *, pelican_population_dynamics
print *, is_young_pelican
print *, pelican_tag
end program variables
The output below is from the GNU gfortran compiled executable:
OUTPUT
-922534656
0.00000000
(2.063298560E+11,0.00000000)
T
Where did those values come from? If you forget to assign a value to a variable the output will depend on your compiler. Here the program accessed the memory allocated for each variable and printed what was leftover in the memory from other processes.
Most compilers have a flag which warns you if there are uninitialised variables in your code. Have a look and see if there is a flag like this for your compiler.
- Fortran is case-insensitive. This course prefers the use of lowercase.
- Fortran file names must match the name of the program or module
contained in the file. ie.
variables.f90
contains the programvariables
.matrix_mod.f90
contains the modulematrix_mod
. -
::
markers should be aligned to improve readability.
Variable Assignment
Variables are assigned using the assignment operator
=
:
For example:
FORTRAN
number_of_pelicans = 5
pelican_weight = 2.5 ! kg
pelican_population_dynamics = (-1.2, 0.9)
is_young_pelican = .false.
pelican_tag = 'Jeff'
Logicals can be .true.
or .false.
.
Characters (strings) such as pelican_tag
can be surrounded
by single or double quotes.
Modify your variables
program
- Assign values to the variables in your program.
- Compile and run your program.
FORTRAN
program variables
implicit none
integer :: number_of_pelicans
real :: pelican_weight
complex :: pelican_population_dynamics
logical :: is_young_pelican
character :: pelican_tag
number_of_pelicans = 5
pelican_weight = 2.5 ! kg
pelican_population_dynamics = (-1.2, 0.9)
is_young_pelican = .false.
pelican_tag = 'J'
print *, number_of_pelicans
print *, pelican_weight
print *, pelican_population_dynamics
print *, is_young_pelican
print *, pelican_tag
end program variables
Example output:
OUTPUT
5
2.50000000
(-1.20000005,0.899999976)
F
J
- The assignment operator
=
should be aligned to improve readability. - Variable names are written in snake case and are verbose.
- Variables with units must have a comment (Ford or vanilla Fortran style) with the unit.
Assignment on declaration
Fortran lets you assign a value to a variable when you declare it.
This gives the variable the save
attribute. With
save
the variable will keep its value between procedure
(function) calls. This is not good practice. Never assign a value to a
variable on declaration unless it’s a parameter.
Parameters
In your program, it is possible to change the values of initialised variables, e.g.
FORTRAN
program variables
implicit none
integer :: number_of_pelicans
real :: pelican_weight
complex :: pelican_population_dynamics
logical :: is_young_pelican
character :: pelican_tag
number_of_pelicans = 5
pelican_weight = 2.5 ! kg
pelican_population_dynamics = (-1.2, 0.9)
is_young_pelican = .false.
pelican_tag = 'J'
! print *, number_of_pelicans
! print *, pelican_weight
! print *, pelican_population_dynamics
! print *, is_young_pelican
print *, pelican_tag
! Changing the value of the tag
pelican_tag = 'F'
print *, pelican_tag
end program variables
This will now give the output:
OUTPUT
J
F
However, you can also define constant values that cannot change. You
do this by defining variables using parameter
. We are then
unable to modify parameter
variables, e.g.
Add a parameter to your variables
program
- Modify a variable in your code to be a parameter.
- Try modifying the parameter in your code. What output do you get when compiling?
FORTRAN
program variables
implicit none
integer :: number_of_pelicans
real :: pelican_weight
complex :: pelican_population_dynamics
logical :: is_young_pelican
character, parameter :: pelican_tag = 'J'
number_of_pelicans = 5
pelican_weight = 2.5 ! kg
pelican_population_dynamics = (-1.2, 0.9)
is_young_pelican = .false.
! pelican_tag = 'J'
print *, number_of_pelicans
print *, pelican_weight
print *, pelican_population_dynamics
print *, is_young_pelican
print *, pelican_tag
pelican_tag = 'F'
end program variables
Here we have modified pelican_tag
to be the parameter.
Then at the end of the program we attempt to change its value.
Example GFortran output:
OUTPUT
variables.f90:23:4:
23 | pelican_tag = 'F'
| 1
Error: Named constant ‘pelican_tag’ in variable definition context (assignment) at (1)
The compiler has given us an error. This is because we are trying to
edit the value, variable definition context (assignment)
,
of a parameter, Named constant
. The error is in the
variables.f90
file, on line 23
, starting at
character 4
. This location has been marked as
1
in the compiler output.
Different compilers will show different error messages. Some have clearer messages for certain errors than others. We recommend testing code with at least two compilers. This will aid your debugging and help make your code more portable.
Tidy up your program
- Make sure your code conforms to the style followed by this course.
- Add Ford comments to document the program and each variable.
FORTRAN
program variables
!! A test program to lean how to declare and assign variables.
implicit none
integer :: number_of_pelicans
!! The number of pelicans in the pod
real :: pelican_weight
!! The average weight of a pelican in the pod / kg
complex :: pelican_population_dynamics
!! The birth and death rate as a complex number
!! Units are the number of pelicans per year
logical :: is_young_pelican
!! Test to see if the current pelican is young
character, parameter :: pelican_tag = 'J'
!! Pelican pod tracking tag
number_of_pelicans = 5
pelican_weight = 2.5 ! kg
pelican_population_dynamics = (-1.2, 0.9) ! births, deaths per year
is_young_pelican = .false.
print *, number_of_pelicans
print *, pelican_weight
print *, pelican_population_dynamics
print *, is_young_pelican
print *, pelican_tag
end program variables
Notice we have left an extra blank line in-between the parameter
declaration and the other variable declarations. This is so we didn’t
have to align all the ::
markers far to the right. If you
are declaring lots of variables, break up the declarations into sections
for readability.
Key Points
- There are 5 intrinsic data types for Fortran variables:
integer
,real
,complex
,logical
, andcharacter
. - Fortran variables are declared with the syntax:
<variable_type> :: <variable_name>
- Assign a value to a variable with the syntax:
<variable> = <value>
- Never assign a value on the same line as a variable is declared.
This gives the variable the
save
attribute. - Parameters are variables whose value can’t be changed:
<variable_type>, parameter :: <variable_name> = <variable_value>
.
Fortran is also statically typed. You can not change a variables type after the variable declaration.↩︎