2.2. Quick Tour

2.2.1. Hello World

If the "Hello World" program tests the simplicity of a programming language, Fortran scores quite high in this contest. In general, Fortran's syntax keeps programs relatively short. In our first example, just the asterisk as the first argument of the print statement is not immediately clear from the context (it is a wildcard for the print format).

      PROGRAM HELLO
      PRINT *, 'Hello World'
      END

Apart from string literals, Fortran is case insensitive, and identifiers are normally written with uppercase characters. In Fortran77, only the first 6 characters of an identifier are significant (Fortran90: 31 characters).

Of course, we are back to punch cards again. The program text is devided into lines of 72 characters (everything beyond the 72nd character is ignored). If a line starts with an asterisk or a C it is considered a comment.

C This is the Hello World program.
      PRINT *, 'Hello World'
      END

Otherwise, the first five columns are reserved for statement labels and the sixth column for the continuation markers. The actual statements start in the seventh column.

If a statement spans multiple lines, the continuation lines are marked by some character in the sixth column (often a $ sign, but any character is fine).

Statement labels are integer values used targets for jumps and loops. Fortunately, we hardly need statement labels anymore when using the latest version of Fortran. The following example echos the user's input (must be numbers) in an infinite loop.

 100  READ *, X
      PRINT *, 'X=', X
      GO TO 100
      END

After this short excursion into the formatting of the program text, let's get back to the language itself. Aimed at numerical computations, Fortran fully supports arithmetical expressions with correct precedences, exponentiation, and many built-in mathematical functions. The last print statement demonstrates complex literals with the read and imaginary part written as a pair (2i squared is -4).

      PRINT *, 2 + 1.5 * 3 - 2 ** 3
      PRINT *, 2 * ASIN(1.0)
      PRINT *, (0, 2) ** 2
      END

result:
 -1.5
  3.14159274
 (-4.,0.)

2.2.2. Expressions and Variables

In Fortran, variables do not have to be declared in advance. However, they are strongly typed. How is this possible? If a variable is not declared explicitly, the type is derived from the first character of the variable's name. By default, all variables starting with with a character between "I" and "N" are integers, and all others are real numbers.

      I=55
      X=55
      PRINT *, I, X
      END

result:
 55  55.

Note the period in the output indicating the real number. The implicit typing rules can be changed using the IMPLICIT statement.

      IMPLICIT INTEGER(X-Z)
      X=55
      PRINT *, X
      END

result:
 55

In general, Fortran does a lot implicitly, which keeps programs short, but sometimes also leads to surprises. Assigning a real number to an integer, for example, automatically truncates the number.

      I=5.6
      PRINT *, I

result:
 5

To be on the safe side, it is better to declare variables explicitly. This is done by stating the type followed by the names of the variables.

      DOUBLE PRECISION X, Y
      X=5.6
      Y=1.5E-10
      PRINT *, X, Y
      END

result:
  5.5999999  1.49999999E-10

Table 2-1> lists the six types available in Fortran.

The two boolean values are called .TRUE. and .FALSE.. Fortran77 has a very limited character set which does not even contain the < or > symbol. Therefore, comparison operators are denoted by shortcuts for the english expressions. .LT., for example, means "less than" and .NE. means "not equal". Fortran99 now also provides the usual operator such as <= and /=.

      B=X .LE. 5.0 .AND. 5 > 4) 

2.2.3. Arrays

Since vectors and matrices play a very important role in numerical computations, arrays are one of Fortran's strengths. To define an array, we add the dimensions to the variable declaration. Indexing starts at one.

2.2.4. Functions

Fortran uses the function name as a variable. This means that we define the return type just like we define a variable, and we can use the function name to manipulate the return value just like any other variable.

      PROGRAM FUNCTION_TEST
      PRINT *, '1.5 + 2.5=', ADD(1.5, 2.5)
      END
      FUNCTION ADD(X, Y)
      REAL X, Y, ADD
      ADD = X + Y
      END