Types of Variables

FORTRAN provides several predefined types of variables for our use: INTEGER, REAL, LOGICAL, CHARACTER and COMPLEX. These notes defer optional features and the COMPLEX type until the end.


INTEGER type

An INTEGER variable contains a signed binary value of some number of bytes. For the compiler we are using, this is 4 bytes by default. The values of a 4-byte INTEGER are in the range -2**31 to 2**31 - 1, that is, roughly -2 billion to +2 billion.


REAL type

A REAL variable contains a floating-point value of some number of bytes. For the compiler we are using, this is 4 bytes. A floating point number is stored an "scientific notation", that is:

          fraction * 10**exponent

A 4-byte REAL is stored as 32 bits:

         bit 0 (leftmost)    sign bit
         bits 1 through 7    exponent
         bits 8 through 31   fraction

With a 4-byte REAL, we have about 6 or 7 decimal digits of precision. and the exponent has values approximately in the range -38 to +38. If more digits are printed beyond those that are known to be valid, they should not be trusted.


LOGICAL type

A LOGICAL variable contains one of two values, .FALSE. or .TRUE..


CHARACTER type

A CHARACTER variable stores a "character string,", that is, a sequence of characters such as a name, a phrase or a sentence. When we write out a string, we enclose it in single quotation marks (') or in double quotation marks (").

A CHARACTER variable has a length, the number of characters involved. We can specify the length in several ways:

          CHARACTER        (same as length 1)
          CHARACTER(n)     (where n = the desired length)
          CHARACTER(LEN=n) (same as the preceding)

What is actually stored internally? Each character is stored as a small (1 byte) integer value, 0 to 255. These small numbers correspond to letters, digits, etc., according to the ASCII character sequence. For instance, a blank space corresponds to ASCII value 32.

The maximum possible length of a CHARACTER variable depends on the specific compiler used. (Sometimes 500 has been used.)


How do we declare variables?

We declare variables as follows:

         typename :: variablename
or
         typename :: variablename = value
if we want to initialize the variable.

We can declare several variables of the same type at once:

         INTEGER :: ABC = 3, GHI = 7, JKL
         REAL :: X, Y = 17.8, Z

We can declare a constant by using the keyword PARAMETER:

         REAL, PARAMETER :: E = 2.71828
         INTEGER, PARAMETER :: MAXLENGTH = 31

Here "constant" means what it says: we cannot change the value later. Constants must be initialized at once, as we will not have a second chance.


What values can we use to initialize a variable or constant?

The value we use in initializing an INTEGER variable is a more or less ordinary-looking number. We have a sequence of digits (0-9) with perhaps a sign at the left side. If there is no sign, or if the sign is a plus sign (+), the value is positive. If the sign is a minus sign (-), the value is negative. An INTEGER value does not have a decimal point.

The value we use in initializing a REAL variable is similar, but we do have a decimal point, and we can have digits on both sides of it.

The value we use in initializing a CHARACTER variable is a "string", that is, a sequence of characters set off in single quotes (') or double quotes ("). We could even have a single quote in a string as long as the string itself is set off in double quotes, as in "My name is O'Hara.". The enclosing quotation marks are not part of the value; they are just punctuation called "delimiters".

The only values available for LOGICAL variables are .TRUE. and .FALSE..


Implicit naming convention

An interesting and old-fashioned feature of FORTRAN is that we do not absolutely have to declare our variables. If a variable is not declared at all, then it is regarded as REAL or INTEGER depending on the first letter in its name:

     1st letter = 'I' through 'N':  assumed to be INTEGER
     1st letter = any other letter:  asssumed to be REAL

This is known as the "implicit naming convention" and is not nowadays regarded as a good idea. We can and should switch it off by including the line

         IMPLICIT NONE
at the top of each program.


COMPLEX type

We can also declare variables of type COMPLEX. This corresponds to complex numbers in mathematics, which may or may not be familiar to students in the course. A complex number is of the form A + Bi, where i = the square root of -1, which is sometimes called "imaginary 1".

We declare a COMPLEX variable as follows:

         COMPLEX Z

The variable Z will have a "real part" and an "imaginary part" and should be envisioned as a pair of REAL values (A, B) corresponding to A + Bi. There are built-in facilities for obtaining the two parts of Z and other useful imformation:

         REAL(Z) is the "real part", or A.

         AIMAG(Z) is the "imaginary part", or B.

         ABS(Z) is SQRT(A*A + B*B), the "magnitude" of Z.

         CONJG(Z) is (A, -B), the "complex conjugate" of Z.

We can create a COMPLEX value from two REAL values:

          Z = CMPLX(a, b)

If we READ a COMPLEX variable from standard input, as in

          READ (*, *) Z

the data should be in the form (a, b), as in (1.00,2.00).

Likewise, if we want to PRINT a COMPLEX value, as in

          PRINT *, Z

the default format is of the form (a, b), as in (1.000000,2.000000).

If you are not familiar with complex numbers, don't worry about it. We don't do much (if anything) with COMPLEX variables in this course.


Other kinds of numbers

What if we happen to need numbers with more digits than we have with INTEGER or REAL? FORTRAN provides a way to handle this need.

We can define an INTEGER or a REAL with a parameter called KIND. If we don't use it, we get the kind of INTEGER or REAL described above. The possible values of KIND depend on the compiler to some extent.

On our system, a REAL variable has these possibilities:

On our system, an INTEGER variable has these possibilties:

Examples of how we use these:

         INTEGER (KIND = 1) :: LITTLEINT
         INTEGER (KIND = 4) :: NORMALINT   ! same as INTEGER :: NORMALINT
         INTEGER (KIND = 16) :: HUGEINT

         REAL (KIND = 4) :: NORMALREAL     ! same as REAL :: NORMALREAL
         REAL (KIND = 8) :: HUGEREAL