We have explained elsewhere how data is stored in a computer. Now we will have to consider how it is represented in a Fortran program. Here are the possibilities:
5 or 'ABC'.
VAR.
SIN(0.5).
0.5*SIN(0.3).
One of the most important concepts in programming is that of a variable: a named container for values. Here we will take a look at the rules for valid names of a variable, and the declaration of what type of data it can hold. Elsewhere we will look at the actual assigning of a value to a variable.
According to the Fortran77 standard, an identifier or variable name, can be up to 6 characters long, where each character is a letter or number, and the first character has to be a letter. It does not matter whether the letters are uppercase or lowercase.
In practice, many systems allow you to have longer variable names, and special characters such as an underscore may be allowed.
Spaces, anywhere in the program (but limited to columns 7-72) are ignored, in particular in variable names.
Fortran is a typed language: variables have a data type associated with them. In other words, the same variable can not store a complex number at one time, and a character at another. There are several rules for declaring the type of a variable. The main type of declaration looks like:
DATATYPE LIST-OF-NAMES
This will be discussed in more detail later.
Unlike most other programming languages, Fortran has no reserved words.
That is, it is perfectly legal to name your variables END or
PROGRAM. It is not a smart idea to do so.
Fortran has numeric data types (integer, real, complex), character data, and logical data.
There are 3 separate numeric data types. Various conversions are possible between them.
The integer data type is specified by the keyword INTEGER
in a declaration statement. The range of integers
you can store in an integer variable is machine dependent. For instance,
an integer can 2 or 4 bytes long.
Many implementations accepted declarations such as
INTEGER*2 X
that explicitly declare the number of bytes allocated. The usual values are 2 and 4, and sometimes 1.
Integer constants are written as a string of digits, possibly preceded by one unary minus or plus sign. Examples:
555 -678 +9
Fortran operator rules prohibit more than
one unary plus or minus, but writing -(-9) is allowed.
The real data type is specified by the keywords REAL or DOUBLE
PRECISION in a declaration. Typically,
a real variable takes 4 bytes, and a double precisions variable takes 8.
The main reason for using double precision variables is numerical accuracy:
the machine constant for reals
(or `in single precision') is usually around 10^-6, for double
precision data it is around 10^-15.
Many implementations accepted declarations such as
REAL*4 X
that explicitly declare the number of bytes allocated. The usual values are 4 and 8, and sometimes 16.
Real numbers can be written as integer plus fractional part:
3.141592
or in scientific notation:
31.41592E-1
where the part after the E is the exponent denoting the
powers of ten. Using a D instead of an E makes
this a double precision number:
.314152D1
The complex data type is specified by the keyword COMPLEX
in a declaration. A complex value consists
of a pair of REAL variables. It can be written as
(1.5,-2.3)
that is, using two real constants.
The character data type is specified by the keyword CHARACTER
followed by an asterisk and a length in a declaration.
Example:
CHARACTER*20 A
declares A to a string of at most 20 characters. In
CHARACTER*20 A,B,C*10
the variables A and B are 20 characters long,
but C is ten long.
Character constants are written as a string of characters delimited by single quotes (apostrophes):
'ABC 12D'
In order to get an apostrophe in a string, you need to write two of them in a row.
The logical data type is specified by the keyword LOGICAL
in a declaration. There are just two possible
logical values, written as
.TRUE. .FALSE.
Expressions combine constants and variables to form values, for instance for use in assignment statements:
x = <expression>
or in output statements:
PRINT *,<expression>
or in control structures:
DO 10 i=<expression1>,<expression2>
...
IF ( <logical expression> ) THEN
Fortran numeric expressions can have the usual four operators: +,-,*,-.
Exponentiation is done by the ** operator. You can use parentheses
and unary plus or minus signs. The usual operator precendence rules hold,
and operators with equal precedence are evaluated from left to right.
No two operators can follow each other. In particular, this prohibits
writing two unary minus signs in a row. Also, writing X**-2
is not allowed; the correct form is X**(-2).
If an expression contains constants or variables of more than one numeric type (eg, an integer and a real, or a real and a complex), there will be a conversion to the most comprehensive type.
This conversion can be tricky: in the exression 2.0*(1/2)
the fraction is first evaluated as an integer expression, giving zero. Thus
the final result is zero. Writing 2.0*(1./2) or 2.0*(1/2.)
gives one as result. (But note that 3.0*(1./3) will likely
not give exactly one: the fraction 1/3 is not exactly
representable as a binary fraction.)
The only operations on logical values and variables are
.AND. .OR. .EQV. .NEQV. .NOT.
Example: if X and Y are logical variables,
then
.NOT. (X .AND. Y)
is a legitimate logical expression. The equivalence operator .EQV.
tests whether two logical values are identical, and .NEQV.
is its negation.
Like the unary minus, the .NOT. operator has a higher precedence
than the binary logical operators. Thus
.NOT.X.AND.Y
is equivalent to (.NOT.X).AND.Y.
The most common way of constructing logical expressions is by arithmetic comparison of numerical values. For instance
i .LE. 17
to test whether variable i is less than or equal to seventeen.
The comparison operators are
.EQ. .NE. .LE. .GE. .LT. .GT.
One can combine comparisons and logical operators, eg
(i .LE. 7) .OR. (i .GE. 10)
While logical expression can be used in the right hand side of assignments:
x = .NOT. y .OR. i.LE.10
the most common use will be in conditionals:
IF ( .NOT. y .OR. i.LE.10 ) THEN
If X is a character variable (or subscripted character array
name), you can take a substring of it by
X(lo:hi)
where lo and hi are integer expressions, within
the bounds of the string. Both bound expressions are optional: when omitted
they are taken to be the bounds of the string. However, the colon is required.
Two strings can be concatenated as
'AB'//'CD'
giving 'ABCD'. Be careful using character variables as operands
of the concatenation operator:
CHARACTER*5 A
A = 'BC'
X = A // 'DE'
gives 'BC DE' in X.
In order to pass a library function as an
actual argument, it needs to be declared as INTRINSIC first:
INTRINSIC sin
...
res = integrate(sin,0.0,1.0)
The INTRINSIC statement is a specification
statement.
Since Fortran has no reserved words, intrinsic function names are not considered intrinsic in a program unit where they also appear as dummy argument.