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 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.
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.
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 :: MOREThese 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:
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.
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).
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.
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.
10 -24 134579 are integers.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.
3.1415926 -3.2E5 0.3456E-57 are real numbers.
-3.2E5 represents -3.2 × 105 = -3.2 × 100,000 = -3,200,000.0
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.
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
Therefore, if IA is assigned the value of 10,
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."
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.