This Appendix is also
available in Swedish
Klammerausdrücke is a German expression, we keep the German expression also in the Russian and English versions. A direct English translation is "bracket expression". FORTRAN 0 was really not called FORTRAN 0, it is just the very first version of Fortran.
The program is given here in Pascal, C and five variants of Fortran. The purpose of this is to show how Fortran has been developed from a cryptical, almost machine-dependent language, into a modern structured high-level programming language.
The final example shows the program in the new programming language F.
program tpk(input,output);
(* Pascal program for Unix *)
var i : integer;
y : real;
a : array [0..10] of real;
function f (t : real) : real;
begin
f := sqrt(abs(t)) + 5*t*t*t
end;
begin
for i := 0 to 10 do read(a[i]);
for i := 10 downto 0 do
begin
y := f(a[i]);
if y > 400 then
writeln(i,' TOO LARGE')
else
writeln(i,y);
end
end.
This program contains variables of the data types integer and
floating-point numbers, and a vector of floating-point numbers. It
also contains internal mathematical functions and a function written
by the user, both forward and backward loops, a conditional statement
and output of both floating-point numbers and characters.
# include <stdio.h>
# include <math.h>
/* Program TPK in ANSI C */
double f (double t);
main()
{
int i;
double y;
double a[11];
for ( i = 0; i <= 10; ++i)
scanf("%lf", &a[i]);
for ( i = 10; i >= 0; i = i -1 )
{
y = f(a[i]);
if ( y > 400 )
{printf("%d",i);
printf(" TOO LARGE\n");}
else
{printf("%d",i);
printf("%lf",y);
printf("\n");}
}
return 0;
}
/* Function */
double f (double t)
{
double temp;
temp = sqrt(fabs(t)) + 5*pow(t,3);
return temp;
}
The language C had the peculiar property that function calls were
normally done in double precision, I have therefore written the whole
program in double precision. This is not really necessary, because
nowadays you can also use the single precision in most realizations.
The exponentiation can be done here with the internal function pow.
The Fortran inventor John Backus in New Mexico, 1976
DIMENSION A(11)
READ A
2 DO 3,8,11 J=1,11
3 I=11-J
Y=SQRT(ABS(A(I+1)))+5*A(I+1)**3
IF (400>=Y) 8,4
4 PRINT I,999.
GOTO 2
8 PRINT I,Y
11 STOP
Please note the elegant treatment of the input of the vector A and
that the symbol > (greater than) was available at the beginning of the
Fortran development. Output of text was not available, therefore 999
was used here to mark too big numbers. The DO-loop was less elegant, the
digits give starting line and the final line for the loop and where
the execution should be transferred when the loop is terminated.
The conditional statement is also not very user-friendly.
Exponentiation is being done with **.
C THE TPK ALGORITHM
C FORTRAN I STYLE
FUNF(T)=SQRTF(ABSF(T))+5.0*T**3
DIMENSION A(11)
1 FORMAT(6F12.4)
READ 1,A
DO 10 J=1,11
I=11-J
Y=FUNF(A(I+1))
IF(400.0-Y)4,8,8
4 PRINT 5,I
5 FORMAT(I10,10H TOO LARGE)
GOTO 10
8 PRINT 9,I,Y
9 FORMAT(I10,F12.7)
10 CONTINUE
STOP 52525
This was the first programming language with comments. The
statement function is used and the backward loop has to be simulated,
since a backward loop (negative index) was not permitted, and neither
was index zero. The conditional statement is the arithmetical one,
with jumps to three different positions depending on whether the
arithmetic expression is less than zero, equal to zero or greater than
zero. All function names have to end with an F. It is possible to
print character strings if you can give the number of characters which
will be output (10H in the case when ten characters are to be
outputted). Structured layout had not yet been invented.
C THE TPK ALGORITHM
C FORTRAN IV STYLE
DIMENSION A(11)
FUN(T) = SQRT(ABS(T)) + 5.0*T**3
READ (5,1) A
1 FORMAT(5F10.2)
DO 10 J = 1, 11
I = 11 - J
Y = FUN(A(I+1))
IF (400.0-Y) 4, 8, 8
4 WRITE (6,5) I
5 FORMAT(I10, 10H TOO LARGE)
GO TO 10
8 WRITE(6,9) I, Y
9 FORMAT(I10, F12.6)
10 CONTINUE
STOP
END
Also here a statement function is being used and the backward loop
is still simulated. The structured layout has been used. The notation
Fortran 66 started to be used in 1978, previously it was just called
Standard FORTRAN or the IBM-notation FORTRAN IV was used also by other
vendors.
PROGRAM TPK
C THE TPK ALGORITHM
C FORTRAN 77 STYLE
REAL A(0:10)
READ (5,*) A
DO 10 I = 10, 0, -1
Y = FUN(A(I))
IF ( Y . LT. 400) THEN
WRITE(6,9) I,Y
9 FORMAT(I10. F12.6)
ELSE
WRITE (6,5) I
5 FORMAT(I10,' TOO LARGE')
ENDIF
10 CONTINUE
END
REAL FUNCTION FUN(T)
REAL T
FUN = SQRT(ABS(T)) + 5.0*T**3
END
Also here a statement function ought be used, but is was not
popular at that time, and therefore an external function was being
used instead. The backward loop no longer has to be simulated and the
conditional statement can be given in a more natural way. Structured
layout is now the normal case. The character string can be given
within apostrophes so that you no longer have to count manually the
number of characters in the character string. The statement END in
the main program is also being used for the execution so that STOP is
no longer required. Also the explicit RETURN in the
function FUN is
now optional.
PROGRAM TPK
! The TPK Algorithm
! Fortran 90 style
IMPLICIT NONE
INTEGER :: I
REAL :: Y
REAL, DIMENSION(0:10) :: A
READ (*,*) A
DO I = 10, 0, -1 ! Backwards
Y = FUN(A(I))
IF ( Y < 400.0 ) THEN
WRITE(*,*) I, Y
ELSE
WRITE(*,*) I, ' Too large'
END IF
END DO
CONTAINS ! Local function
FUNCTION FUN(T)
REAL :: FUN
REAL, INTENT(IN) :: T
FUN = SQRT(ABS(T)) + 5.0*T**3
END FUNCTION FUN
END PROGRAM TPK
Instead of an external function a local function is now used. The
backward loop has not to be simulated. The conditional statement can
be given in a more natural way and the symbol < (less than) has
returned from Fortran 0. The specifications have a new appearance.
Structured layout is used always and all statement numbers have been
removed. The character string is given within apostrophes and the
standard formats and standard units are used. Comments can be
given on the same line as the statement, and comments are now indicated
by an exclamation mark ! Implicit
specification can (and should) be avoided.
This example is not changed in Fortran 95!
module Functions
public :: fun
contains
function fun(t) result (r)
real, intent(in) :: t
real :: r
r = sqrt(abs(t)) + 5.0*t**3
end function fun
end module Functions
program TPK
! The TPK Algorithm
! F style
use Functions
integer :: i
real :: y
real, dimension(0:10) :: a
read *, a
do i = 10, 0, -1 ! Backwards
y = fun(a(i))
if ( y < 400.0 ) then
print *, i, y
else
print *, i, " Too large"
end if
end do
end program TPK
The new language F has been developed as a possible replacement of Pascal at the teaching of programming, but at the same it is a pure subset of Fortran 90 and Fortran 95.
A free F compiler is now available.
Keywords are reserved words and are required to be lower case letters, other identifiers may be mixed. Functions and subroutines are only permitted via modules. By default all variables have to be declared (the statement IMPLICIT NONE is therefore implicit, and was previously not permitted explicitly, it is however now optional). When available, the compiler switch -u is therefore very useful if you run an F program with a Fortran 90 compiler.
This example is a first attempt at writing in F!