Fortran Introduction and Resources
If you have any experience with programming, you can learn Fortran in a couple of hours. It is an amazingly easy language, yet very powerful. And to give you some idea of that power, consider this: it first appeared in 1957 and is still used today to solve some of the most complicated problems in modern science and engineering.
A Very Brief History of Fortran
In order to understand the history of Fortran, we have to go back to the earliest modern computers.
In the 1940s, the computers were all programmed with assembly language — giving the computer direct instructions. For example, imagine you wanted to add two integers on 1954's cutting edge computer the IBM 704. You would have to write something like this (pdf):
CLA tells the computer to clear the Accumulator Register and add to it the value stored at memory location 100. Then the ADD command tells the computer to add the value stored at memory location 101 to the value stored at the Accumulator Register. This is already complicated and we haven't even discussed how to store the values at memory locations 100 and 101!
Clearly, this kind of coding is laborious and incredibly prone to error. So in late 1953 — shortly before the IBM 704 was to be released — computer scientist John W Backus submitted a proposal to create a high level language that would allow programmers to write simple statements like
I = 10 + J. Then the compiler could convert this into the many lines of assembly language the computer required. And by late 1956, the first FORTRAN manual (pdf) was released. And six months later, the first compiler to convert Fortran into assembly language was released.
FORTRAN was not the first high level programming language. But it was the first one to be widely used. And there are good reasons for that.
Although the first Fortran compiler was released in 1957, the language continued to grow and evolve.
Released in 1958, FORTRAN II introduced subroutines and functions. These were critical facilities because they allowed for the development of well structured code. These features allowed programs to call the same blocks of code to be called multiple times without repeating the code or using GOTO calls. The main differences between these two things is that subroutines did not return variables like functions did.
FORTRAN IV / FORTRAN 66
FORTRAN IV was a very important milestone. In particular, it removed machine-dependent features and introduced logical (Boolean) features to the language. But FORTRAN IV was more important because it became more or less the standard FORTRAN for the programming community. This was most due to the fact that FORTRAN 66 was released right after FORTRAN IV. And FORTRAN 66 was the first version to haven an official American National Standards Institute (ANSI) version.
Because of its timing, FORTRAN 77 is the version most people think of when they think of the language. It added a number of important functions. For example, although FORTRAN IV had
IF statements (including three-way
IF statements), it wasn't until FORTRAN 77 that
ELSE IF functions were allowed. It also greatly advanced file I/O and character processing abilities.
This represented a huge change in the language as can be seen simply by the fact that the name of the language was no longer printed with all capital letters. It allowed Fortran commands to be written in lower case and implemented free-form source input. But it also implemented much more sophisticated features like operator overloading. At the same time, it did not remove any features, so that Fortran 77 programs still worked exactly as they had before.
There have been a number of updates to the language since Fortran 90: Fortran 95, Fortran 2003, Fortran 2008, and the upcoming Fortran 2015. All of these are minor updates. Nothing substantial has happened to the language since Fortran 90, or even FORTRAN 77 since its code will still generally compiles without change.
Why Do People Use Fortran?
If you want to perform numerical calculations, there really isn't a better language. Because it has been around so long, Fortran compilers have been optimized to perform calculations. But perhaps the best reason to use Fortran is that there is an enormous amount of existing code for the sorts of things that scientific programmers do all the time.
Consider this: Numerical Recipes: The Art of Scientific Computing has been an essential book for all scientific programmers for decades. When it first came out in 1986, it was written with all Fortran examples. There was no mention of this fact on the cover. It was just assumed. It took three years before a special edition of the book came out with Pascal code. It took six years before a C language version came out.
What Makes Fortran So Easy?
Basic Fortran only does simple things. All the aspects of it like loops and if-then statements are so integrated in other languages that it is unlikely that you will have any trouble picking it up. The only thing about it that might be a bit difficult is that Fortran requires a highly structured input format — or at least Fortran 66 and Fortran 77 did. Fortran 90 and onward is far more flexible. But if you are working with Fortran, chances are that you will dealing with the older versions of the code.
Fortran 77 Example
program sequence c Lines that start with c (or *) are comments * This program will print out a simple arithmetic series integer n, i n = 0 do 10 i = 1, 5 n = n + i write(*,*) "The ", i, "number in this series " +"is: ", n 10 continue stop end
There are a few things to notice here:
- Regular code must start on the seventh column (that is, with six spaces before it).
- If a "c" or an "*" is placed in the first column, it is a comment and not compiled.
- Columns 1-5 can be used for statement labels — similar to old style BASIC.
- If a character is placed in column 6, it means that the code from the previous line is continued. This is important because statements can only be placed from the 7th to the 72th column.
The output should be pretty clear:
The 1 number in this series is 1
The 2 number in this series is 3
The 3 number in this series is 6
The 4 number in this series is 10
The 5 number in this series is 15
You have a few alternatives in how you learn Fortran.
Fortran really is a very simple language to learn if all you need to do is create programs for solving numerical problems. Clearly, it is not the choice of people writing graphical user interfaces. But for scientists and engineers, Fortran is often the best tool for the job. With these tutorials, you will probably be able to be ready to write your own useful programs within a couple of hours.
- FORTRAN 77 Tutorial: the Stanford University tutorial that shows you all you need to know to start coding FORTRAN 77. And if you know FORTRAN 77, you should be able to use just about any modern Fortran compiler.
- 7 Lessons With Fortran 90/95: if you'd like to start with the newer standard and not have to worry about the rigorous formatting required by earlier versions, this is the place to start. It also offers a PDF manual that you can use instead.
- Introduction and Basic Fortran: old style Fortran with lots of examples.
- Fortran Tutorial: the Tutorials Point tutorial using the G95 Fortran compiler.
- Programming in FORTRAN: a very basic introduction to FORTRAN 77, but likely all you really need.
There are a surprising number of books about the Fortran programming language. You may find these the quickest and easiest way to get started with the language.
- Fortran Programming Success in a Day by Sam Key: a short book that delivers on the promise of its title.
- FORTRAN 90/95 for Scientists and Engineers by Stephen J Chapman: a thorough introduction to modern Fortran programming with an emphasis on its use in the sciences.
- Migrating to Fortran 90 by James Kerrigan: a book to get people who are familiar with the old style of Fortran up and running with Fortran 90. It is also an excellent reference.
There are a number of compilers available. See Wikipedia's list of Fortran compilers. A few deserve special discussion:
- GNU Fortran: the official Fortran compiler (compatible with Fortran 95) that is part of the GNU Compiler Collection.
- G95: a free Fortran 95 compiler that is in active development and is implementing parts of Fortran 2003 and 2008.
- MinGW: a port of the GNU Compiler Collection (including Fortran) for the purpose of creating Microsoft Windows applications.
- F2c: a simple program and library for converting Fortran programs into C. This is useful for C and C++ programmers who need to use existing Fortran code, but don't want to bother with the language.
- Fortran Tools, Libraries, and Application Software: The Fortran Company's list of tools and libraries (most free) for helping Fortran coders. It includes things like graphics libraries.
- Simply Fortran: an inexpensive Linux and Windows-base compiler, which includes an integrated development environment and debugger.
- Modern Fortran: Style and Usage by Clerman and Spector: a book to help scientific coders use Fortran efficiently.
- Numerical Recipes in Fortran 77: The Art of Scientific Computing: still the ultimate source for the most important algorithms.
Even though it is very old, Fortran remains a valuable tool for programmers — especially those working in science and engineering. At the same time, it is an easy language to use. So either as a first language, or as another tool to add to your arsenal, it is worth checking out.