Fortran is a language used extensively in the scientific community due to its suitability for numerical computation. Particularly attractive is its intuitive array notation, which makes writing fast vectorised computations easy.
Despite its age, Fortran is still actively developed, with numerous implementations, including GNU, Intel, PGI and Cray.
|FORTRAN 66||First standardization by ASA (now ANSI)||1966-03-07|
|FORTRAN 77||Fixed Form, Historic||1978-04-15|
|Fortran 90||Free Form, ISO Standard, Array operations||1991-06-15|
|Fortran 95||Pure and Elemental Procedures||1997-06-15|
|Fortran 2003||Object Oriented Programming||2004-04-04|
Installation or Setup
Fortran is a language which can be compiled using compilers supplied by many vendors. Different compilers are available for different hardware platforms and operating systems. Some compilers are free software, some can be used free of charge and some require the purchase of a licence.
The most common free Fortran compiler is GNU Fortran or gfortran. The source code is available from GNU as a part of GCC, the GNU compiler collection. Binaries for many operating systems are available at https://gcc.gnu.org/wiki/GFortranBinaries. Linux distributions often contain gfortran in their package manager.
Further compilers are available for example:
EKOPath by PathScale
All these compilers support the Fortran 95 standard. An overview on the Fortran 2003 status and the Fortran 2008 status by various compilers is offered by the ACM Fortran Forum and available in the Fortran Wiki.
Any Fortran program has to include
end as last statement. Therefore, the simplest Fortran program looks like this:
Here are some examples of “hello, world” programs:
print *, "Hello, world" end
write(*,*) "Hello, world" end
For clarity it is now common to use the
program statement to start a program and give it a name. The
end statement can then refer to this name to make it obvious what it is referring to, and let the compiler check the code for correctness. Further, all Fortran programs should include an
implicit none statement. Thus, a minimal Fortran program actually should look as follows:
program hello implicit none write(*,*) 'Hello world!' end program hello
The next logical step from this point is how to see the result of the hello world program. This section shows how to achieve that in a linux like environment. We assume that you have some basic notions of shell commands, mainly you know how to get to the shell terminal. We also assume that you have already setup your
fortran environment. Using your preferred text editor (notepad, notepad++, vi, vim, emacs, gedit, kate, etc.), save the hello program above (copy and paste) in a file named
hello.f90 in your home directory.
hello.f90 is your source file. Then go to the command line and navigate to the directory(home directory?) where you saved your source file, then type the following command:
>gfortran -o hello hello.f90
You just created your hello world executable program. In technical terms, you just compiled your program. To run it, type the following command:
You should see the following line printed on your shell terminal.
> Hello world!
Congratulations, you just wrote, compiled and ran the “Hello World” program.
Today Fortran is mainly used for numerical computation. This very simple example illustrates the basic program structure to solve quadratic equations:
program quadratic !a comment !should be present in every separate program unit implicit none real :: a, b, c real :: discriminant real :: x1, x2 print *, "Enter the quadratic equation coefficients a, b and c:" read *, a, b, c discriminant = b**2 - 4*a*c if ( discriminant>0 ) then x1 = ( -b + sqrt(discriminant)) / (2 * a) x2 = ( -b - sqrt(discriminant)) / (2 * a) print *, "Real roots:" print *, x1, x2 ! Comparison of floating point numbers for equality is often not recommended. ! Here, it serves the purpose of illustrating the "else if" construct. else if ( discriminant==0 ) then x1 = - b / (2 * a) print *, "Real root:" print *, x1 else print *, "No real roots." end if end program quadratic
Uppercase and lowercase letters of the alphabet are equivalent in the Fortran character set. In other words, Fortran is case insensitive. This behavior is in contrast with case-sensitive languages, such as C++ and many others.
As a consequence, the variables
A are the same variable. In principle one could write a program as follows
pROgrAm MYproGRaM .. enD mYPrOgrAM
It’s to the good programmer to avoid such ugly choices.