FORTRAN - Language features

In this post I’ll look at the FORTRAN 66 language as available on MTS in more detail.

Source code format

FORTRAN originally had a fixed format for source code lines and used all upper case letters; as *FTN does not have this restriction we’ll use lower case and free format in the examples below to make them easier to read, but note that this means these examples may not work on other, non-MTS compilers.

Update 4-Dec-2015: As noted by Mike Stramba, *FTN does require source code to be in upper case, although *IF66 is happy with upper or lower case.

One unique feature is that whitespace is not important, so the variable BXX could also be referred to as B XX. Keywords are context sensitive, so you can write IF IF.GT.3 J=2 where the second IF is actually a variable.

Types and identifiers

FORTRAN supports INTEGER, REAL. LOGICAL and COMPLEX number types. Character types are not support directly, but can be accessed through FORMAT statements (see below).

Variable names can have up to six characters and must start with a letter. Variables can be explicitly declared with their type; if not declared, variables starting with letters I to N are treated as integers, otherwise they are treated as reals.

Variables can be initialised with the DATA statement.

integer count, count1
real temp
complex point
data i,count,temp,point/0, 10, 32.0, cmplx(1,2)/

Arrays and matrices can be created with DIMENSION and initialised with DATA then individual values can be accessed using (), first index 1, in column major order, so in the below X(2, 1) will be intialised with the value 2.0.

dimension x(2,2)
data x/1.0, 2.0, 3.0, 4.0/

Operators and expressions

The usual arithmetic operators appear, including ** for exponentiation. Logical operators look like .LT. for less than or .AND. for Boolean-and.

x = 2.0 ** 4.0
i = 5 + 3
j = x / i
y = x / i

In the result above, J will be set to 2 and y to 2.0.

Statements

Branching can be done with GOTO; there are three forms:

goto 100
goto l
goto (10,20,30) ll

The DO statement allows looping

j = 0
do 100 i=10,20,5
j = j + i
100 continue
k = j * 2

This will initialise I with 10 and then run the contents of the loop for values 10, 15, 20. After that it will jump to the line after the label 100. So for this example, K will end up with the value 90.

It’s possible to nest loops and jump out of loops, but not jump into them.

The IF statement has two forms, logical and arithmetic. The example of the logical version below will set k to 3 if i < j.

if (i.lt.j) k=3

The arithmetic version will branch to one of three labels based on whether the expression being tested is negative, zero or positive. For the example below it will branch to label 30 as the expression is positive.

J = -3
if (j + 4) 10,20,30

Input/output and formats

Inputs (READ) and outputs (WRITE) are associated with a FORMAT statement, a logical I/O unit and a list of variables. For example:

read(5,100) i1,x1,y1
100   format(i2,f5.2,f2.0)

with the input

1234.5678.90

will read from I/O unit 5 (the keyboard) i1=12, x1=34.56, y1=78.0.

Character constants, with a leading length identifier, can be used in READ or WRITE statements as we saw in the hello world example:

write (6,200)
200 format(13h HELLO, WORLD)

prints “HELLO, WORLD” on unit 6, the console.

Functions and subroutines

FORTRAN comes with a large number of built in functions for mathematical processing eg MOD, ABS, SIN. On MTS there is also a library of operating system specific functions that can be called but I will not go into these here.

You can define your own functions to pass parameters by reference and optionally return a value: for example to calculate the average of two values:

real function avg2(x,y)
real x, y
avg2 = (x + y) / 2.0
return
end

This could be called with

z = avg2(3.0, x)

There is also subroutines which run by using call. For example, the below would set x to 2.0:

x = 1.0
call add1(x)

subroutine add1(x)
real x
x = x + 1.0
return
end

Further information

FORTRAN IV reference page is a great summary of FORTRAN 66 syntax.

A contemporary IBM references for FORTRAN can be found at Bitsavers.

MTS Volume 6 describes *FTN and *IF66 in full detail, along with the other FORTRAN tools available.

Comments

comments powered by Disqus