Part 3. Declaring constants and variables

(c) 2017 by Barton Paul Levenson



In a Fortran program (or a Fortran subroutine or function--more about those later), you have to declare all your variables and constants before any "executable" statements. That means you have to have the data in place before you can change it or move it around.

A program has two basic kinds of "data entities"--constants and variables. Constants can't be changed when the program runs, and variables can, thus the names. The variable user in the last part of this tutorial is a sequence of 40 "characters." Characters can be letters, numbers, spaces, digits or punctuation.

This table lists the basic variable types in Fortran:


typecontainslength in bytes (default value in bold)
integer
whole numbers1, 2, or 4
logical
.true. or .false.1, 2, or 4
real
real numbers4, 8, or 10
character
text data0, to thousands
complex
e.g. 3 + 4i8, 16, or 20

The default length of 4 bytes for real numbers is "single precision," accurate to 6 or 7 significant digits. For scientific programming, we prefer "double precision," accurate to 15-16 digits. To get that, we must specify a kind parameter everywhere we declare real variables. We do this by declaring an integer constant for the kind:


    integer, parameter :: d = selected_real_kind(p = 15)


p here means "precision," and asks the underlying hardware for at least 15 significant digits. We can then declare double precision variables with lines such as


    real(d) :: x


The double colons came in with Fortran-90 and are just meaningless "syntactic sugar."

Fortran has "implicit typing" for variables. If you use a variable without declaring it, variable names starting with I through N will be taken as integers, and all others as real numbers.

This is a stupid and dangerous practice. When you misspell a name and the compiler silently treats it as a new variable, it can cause logic bugs which are extremely hard to track down. All my Fortran programs include the directive


    implicit none


just after the program heading, and in every subroutine and function (more about those later). That forces me to declare any variable before I use it.

Here are some examples of variable declarations:


    integer       :: i, j, k
    character(10) :: cityName
    logical       :: switchIsOn
    real(d)       :: magnitude


Note that you can declare more than one variable on a line.

You can also give variables initial values just by following them with an equals sign and the value you want:


    character(10) :: shipName = 'Enterprise'
    integer       :: count    = 0
    real          :: height   = 6.13


The double colons and equals signs don't have to line up, by the way. I just like writing my programs that way. Personal style.

Unlike many other languages, Fortran initializes nothing for you. You have to explicitly give a variable a value before using it, or you will get a run-time "undefined variable" error.

If you want to declare a constant--a value that can't be changed during the operation of the program--you need to add a qualifier to your declaration. There are many such qualifiers, but the one we want here is parameter:


    integer, parameter :: N  = 1000
    real(d), parameter :: e  = 2.718281828459045d0
    real(d), parameter :: pi = 3.141592653589793d0


We need the peculiar d0 suffix on the real-number literals to actually have them treated as double-precision variables. If we were using single precision, we could leave them off. This is a drawback of Fortran.

In general you should declare constants before variables, but it doesn't really matter.





Page created:05/05/2017
Last modified:  05/05/2017
Author:BPL