Chapter 2
Essentials of Fortran-90/95/2003
Variables, Constants, Arithmetic Operations, and Assignment Statements

Math 60 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 60 Homepage | Return to Brief Contents Page]
[Return to Full Contents Page]

Contents


2.1 Variable Identifier Length

As in other computer languages, variable identifiers in Fortran are names for memory locations used to store numeric and other types of data. They may be used to reference simple, scalar quantities, or to reference multiple memory locations (e.g., for arrays, cf. Chapter 8). Variable identifiers follow the rules of any Fortran symbolic identifier.

Starting with Fortran-90/95, symbolic identifiers may be between 1 and 31 letters or numbers for variable and routine identifiers with the first character always being a letter. Fortran 2003 permits identifies up to 63 characters long. The underline symbol, _ , is also permitted as an internal character in any indentifer. (No other special characters are permitted.) As mentioned earlier (Section 1.2), Fortran makes no distinction between upper case and lower case letters of the alphabet.

In FORTRAN-77 (and earlier versions of Fortran), symbolic identifiers are limited to a maximum of six letters and numbers in length with the first character always being a letter. (No special characters are permitted.)

When using fixed source form, since all blanks are ignored (except in literal quotation strings), blanks may be included as part of variable names (but this practice is not recommended)!

As examples,

I, A, SAM, NUMBER are valid Fortran variable names,
NUMBERS is not valid in FORTRAN-77.
3AXES, A_#B are not valid identifiers.
New_Value, PROCEDURE_ONE are valid Fortran-90/95/2003 identifiers, but not valid for FORTRAN-77.
(NUMBERS is too long for FORTRAN-77, 3AXES begins with a number, and A_#B contains illegal characters). Also note that in fixed source form, NUMTST is interpreted the same as NUM TST or N UMT S T.

2.2 Data Types, Default Typing, Declarations

Each variable (i.e., memory location) is associated with one type of information or data. Intrinsic Fortran data types are INTEGER, REAL, CHARACTER, LOGICAL, and COMPLEX and users may define other types (cf. Chapter 15). In any subsection of a program, a variable can be associated with only one data type and that type cannot be changed in the course of that program section.

Fortran-90/95 introduced a new way to specify extended-precision numeric data types, that is, numeric data types which can store more significant digits than what is normally allowed by default on a computer. This is done by means of a "kind" specifier discussed in Chapter 14.

Older versions of Fortran included a DOUBLE PRECISION data type which approximately doubled the precision possible with REAL variables. (This type corresponds to the double data type of C/C++.) This data type may continue to be used in Fortran-90/95 as an alternative to declaring variables of an extended-precision "kind" of REAL.

In Fortran, variables do NOT have to be explicitly declared. If a variable is not explicitly declared before use, it is implicitly given a default numeric type based on the first letter of the identifier. (This feature is a carry-over from the first versions of Fortran.) If the first letter is between I and N (inclusive), the variable is considered to refer to an INTEGER quantity. Otherwise, the variable is considered to be REAL. Thus, NUMBER would, by default typing, refer to an INTEGER number, and SAMwould, by default typing, refer to a REAL number.

To declare variables explicitly, one uses a type statement, indicating the data type first, and then the variables of that type (separated by commas). There may be more than one declaration statement of the same type.

Fortran-90/95/2003 style includes a double colon after the type, but this may be omitted to retain compatibility with FORTRAN-77.

(As usual, in fixed source form, the type name must start no earlier than column 7.)

The following is an example of Fortran 90/95/2003 variable declarations:

	REAL      :: A,B,C
	INTEGER   :: I,NUMBER
	INTEGER   :: PETE
	LOGICAL   :: MORE
These lines declare the variables A, B, and C to be of data type REAL, the variables I, NUMBER, and PETE to be of type INTEGER, and the variable MORE to be of type LOGICAL.

NOTES:

  1. Turning off default (automatic) typing: Since variables do NOT have to be declared in FORTRAN, one never gets the compiler error "variable undeclared" as in some other major contemporary languages. Default typing thus means that misspellings and typing errors are not automatically caught. Variables that are not declared (possibly because of typing errors) automatically receive the default numeric data type. To avoid problems associated with default typing, one can make use of the IMPLICIT NONE statement introduced in Fortran-90 and decribed below in section 2.6.

  2. Location of Variable Declarations: Unlike C/C++ or Java, ALL variables must be declared at the beginning of each program segment. After the first executable statement (e.g., an assignment, a function call, a WRITE, an IF, etc.), attempted declarations will result in a compiler error.

  3. Variables are Local in Scope: Unlike in some other popular computer languages, in Fortran each variable is local to the segment in which it is declared. If one wishes to have variables shared between different program segments, one must explicitly list such variables in a COMMON statement (cf. Chapter 18) or declare them in a MODULE (cf. Chapter 9). In Fortran there is no way in which variables can be declared "global" (e.g., by declaring them outside of any segment as in C/C++) and thus be made visible to every subsegment automatically.

2.3 Type Attributes, Parameters (Constants)

Fortran-90/95/2003 permits including other "attributes" before the double colon, combining declaration with parameter value assignment, or array dimensioning (cf. section 8.2), for example. Including attributes in this way eliminates the need to list identifiers several times in the declaration section for different reasons.

We can, thus, write:

     REAL, PARAMETER :: PI = 3.14159265
which declares PI to be of type real as well as identifies it as a PARAMETER (i.e., a constant which cannot be changed -- see section 18.9).

In every case, such "attributes" may be included in the program code as independent statement, but this separate-line style is discouraged, and should be avoided by programmers.

2.4 Initializations in Declaration Statements

In Fortran-90/95/2003, initialization can be combined with variable declaration by using an assignment statement after each declared variable that one wishes to initialize. For example,
      REAL :: A = 1.0, B = 2.0, C = 3.0
(Arrays may also be initialized -- see
section 8.2. Note that in FORTRAN-77, such initialization must be done via a DATA statement -- see section 18.5).

2.5 IMPLICIT Type Statement

One can change the default typing of variable by using the IMPLICIT type declaration statement. For example,
	IMPLICIT INTEGER (A-N)
will cause all variables which begin with any letter between A and N inclusively to be automatically typed as INTEGER.

From the point of view of contemporary programming style, this sort of IMPLICIT statement (and default typing) should be avoided, and each variable should be separately declared.

2.6 IMPLICIT NONE Statement

Default typing can be turned off in any program segment by including the Fortran-90/95/2003 statement:
       IMPLICIT NONE
before any other declaration statement.

When this statement is included, any variable not declared will be caught during compilation and noted as an error (and, thus, typographical errors of variable identifiers will also be caught). Although an optional feature, from the point of view of contemporary programming style, the use of IMPLICIT NONE statements is highly desirable and its inclusion should be considered normal.

Since an IMPLICIT NONE (as with variable declarations) is local to a single program segment, it should be repeated in each program segment in a larger program (i.e., in the main program and any SUBROUTINE or FUNCTION). In FORTRAN-77 and earlier versions, only some compilers recognized IMPLICIT NONE since it was not defined by the FORTRAN standard. As an alternative, some authors advocated using IMPLICIT LOGICAL to detect undeclared (and mistyped) variables since most most variables were for numeric quantities and attempting arithmetic operations with logical variables would lead to a compiler error.

2.7 Constants

Integer constants are written as whole numbers without any decimal point (or commas). Real numbers can be either written in standard (i.e., `floating point') notation (containing a decimal point) or in scientific (i.e., exponential) notation (a number with or without a decimal point, followed by an E, followed by a integer which is the exponent of 10). Integer and real numbers may be preceded by an optional sign. Thus,
10 -24 134579 are integers.
3.1415926 -3.2E5 0.3456E-57 are real numbers.
-3.2E5 represents -3.2 × 105 = -3.2 × 100,000 = -3,200,000.0
Logical constants are discussed in Chapter 5. Character constants are discussed in Chapter 13. Constants for double (extended) precision and complex numbers are discussed in Chapter 14.

2.8 Arithmetic Operations

Fortran arithmetic operators include the standard four operators of addition (+), subtraction (-), multiplication (*), and division (/). Fortran does possess an exponentiation operator, indicated by two asterisks (**). Thus, AB, i.e., A to the power of B, is written in Fortran as "A**B".

The standard algebraic precedence of operations holds, so that exponentiation is performed before multiplication and division, and multiplication and division are performed before addition and subtraction.

When two or more arithmetic operators of equal precedence occur in the same expression, they are evaluated left to right (i.e., as one normally reads the expression), with the exception of two exponentiation operators next to each other. Standard convention is that A**B**C is interpreted as A**(B**C), i.e. the evaluation is done right to left.

Whenever any confusion may occur, one should always force the order of precedence by using parentheses.

2.9 Note on Division

Fortran (as does C/C++) uses a single division operator for division between numbers of whatever data type. If both operands involved are integers, an integer division is performed, with the result that any fractional part is ignored. In other cases, however, the result is numerically appropriate to the two operands (whether they be real, extended precision, or complex). If the two operands have different types, the division is usually performed in the more complicated data type (e.g., a division between integer and real numbers is done in real arithmetic).

The Fortran (and C/C++) division rule can cause unexpected problems when dealing with integer operands. Suppose that IA is declared as an integer variable and assigned the value of 10. Then

IA/20
evaluates to zero, since the integer division of 10 by 20 is less than one and any fractional part is discarded. However,
IA/20.0
evaluates to 0.5 since in this case 20.0 is a real number and a real division is performed (which retains the fractional part).

Therefore, if IA is assigned the value of 10,

3*(IA/20)
evaluates to 0, but
3*(IA/20.)
evaluates to 1.50.

2.10 Assignment Statements

Variables are assigned new values by using the equals sign (=). The variable receiving a new value is placed on the left side of the equals sign. Only a single variable may be on the left side of the equals sign in an assignment statement. Thus,
X = 3.0*A**2 + 2.0*B - C
is a valid Fortran assignment statement.

Since, as in most other computer languages, the assignment replaces the old value of the variable on the left side, a statement such as

    i = i + 1
is legitimate since it means: "take the old value of i and add 1 to it, putting the resulting value into i as its new value."

2.11 Type Conversion

Conversion functions exist to convert between fundamental numeric types, but Fortran-90/95/2003 performs these conversions automatically. Thus, the use of such conversion functions is optional, although in some cases it may be good practice to include such functions explicitly.

In Fortran-90/95/2003, an expression that is real can be assigned to an integer variable. The fractional portion of the number will be discarded, and thus the number is not rounded, but rather truncated.

Similarly, an expression that is integer can also be assigned to a real variable--it is converted automatically.

The older conversion functions (which were mandatory in certain cases in former versions of Fortran) may still be used, and they are mentioned in Chapter 7. In certain case, they are of necessity, for example, when converting two real numbers to one complex number.

For good programming style, the use of conversion functions is recommended in certain instances. As noted above in the case of integer division, one may obtain unexpected results if one assumes that there will be an automatic data type conversion when none, in fact, occurs.


This page is maintained by Dennis C. Smolarski, S.J. dsmolarski@math.scu.edu
© Copyright 1998-2005 Dennis C. Smolarski, S.J., All rights reserved.
Last changed: 23 June 2005.