Fortran
Fortran (also FORTRAN) is a statically typed, compiled, imperative, computer programming language originally developed in the 1950s and still heavily used for scientific computing and numerical computation half a century later. The name is a portmanteau of Formula Translator/Translation. Early versions of the language were known as FORTRAN, but the capitalization has been dropped in newer revisions beginning with Fortran 90. The official language standards now refer to the language as "Fortran".
Although originally a procedural language, recent versions of Fortran have included some features to support object-oriented programming.
History
The first FORTRAN compiler was developed for the IBM 704 in 1954–57 by an IBM team led by John W. Backus. This was an optimizing compiler, because the authors reasoned that no one would use the language if its performance were not comparable to assembly language.
The language was widely adopted by scientists for writing numerically intensive programs, which encouraged compiler writers to produce compilers that generate faster code. The inclusion of a complex number data type in the language made Fortran especially suited to scientific computation. There are many vendors of high performance Fortran compilers today. Many advances in the theory and design of compilers were motivated by the need to generate good code for Fortran programs.
Several standards of the language have appeared: FORTRAN II in 1958, FORTRAN IV in 1961, FORTRAN 66 in 1966, FORTRAN 77 in 1977 , Fortran 90 in 1990, Fortran 95 in 1995, and Fortran 2003 in 2003. Fortran III was designed in 1958, allowing for inline assembler code; but it was never released because the portability concept of a high-level language would be lost. It also included other new features such as boolean expressions. It was used by about 20 IBM customers but was never released as a commercial product. Boolean expressions and IF tests were also included in Fortran IV. Fortran II only had a three way IF branch, based on whether a numeric expression was negative, zero or positive.
Early FORTRAN programs were written on punch cards, and had strict rules for formatting. Line length was limited to 72 columns, originally because this was the maximum number of columns the online punch card reader of the 704 could read. Source statements were punched in columns 7 through 72. The first five columns were reserved for statement numbers or the C in column one that indicated a comment. Statements longer than 66 characters could be continued to additional cards by making a punch in column 6 of the continuation cards. There was a limit on the number of continuation cards allowed, in some implementations as few as three or four. Columns 73 to 80 were often used for sequence numbers, allowing a deck of cards to be resorted if it was dropped. As programs moved to magnetic media, vendors offered different extensions to increase line lengths. FORTRAN 77 was the last version to require these artifacts of computing history.
Fortran 90 was a major revision, adding free source form, dynamic memory allocation, array operations, abstract data types, operator overloading, pointers, and modules to group related procedures and data together. Fortran 95 was a minor revision, adding features for parallel programming from the High Performance Fortran dialect, such as user-defined pure and elemental functions, and the forall construct. The most recent formal standard for the language, published in 2004, is known as Fortran 2003. It is an upwardly-compatible extension of Fortran 95, adding, among other things, support for IEEE floating-point arithmetic, exception handling, object-oriented programming, and improved interoperability with the C language. A comprehensive summary of the 2003 additions is at the ISO Fortran Working Group (WG5) web site, ftp://ftp.nag.co.uk/sc22wg5/N1551-N1600/N1579.pdf.
Features
Initially, the language relied on precise formatting of the source code and heavy use of statement numbers and goto statements. These quirks have been removed from newer versions of the language. Successive versions also introduced 'modern' programming concepts, such as source code comments and output of text, IF-THEN-ELSE (in FORTRAN 77), and parallel constructs, while still attempting to maintain Fortran's 'lean' profile and high performance. Among the most popular specialized Fortran-based languages were SAS, for generating statistical reports, and SIMSCRIPT, for simulating processes involving queuing. F is a clean subset of Fortran 95 that removes the unstructured features of Fortran, such as EQUIVALENCE.
Vendors of high performance scientific computers (Burroughs, CDC, Cray, Honeywell, IBM, Texas Instruments, UNIVAC...) added extensions to Fortran to make use of special hardware features such as: instruction cache, CPU pipeline , vector arrays, etc. For example, one of IBM's Fortran compilers (H Extended IUP) had a level of optimization which reordered the machine code instructions to keep several internal arithmetic units busy at the same time. Another example is CFD, a special 'version' of Fortran designed specifically for the ILLIAC IV supercomputer, running at NASA's Ames Research Center. These extensions have either disappeared over time or had elements incorporated into the main standard; the major remaining extension is OpenMP, which is a cross-platform extension for shared memory programming. One new extension, CoArray Fortran, is intended to promote parallel programming. There is a new programming language under development, Fortress, which is intended to be a replacement for Fortran.
Syntax
As what was basically a first attempt at designing a high-level language, the language's syntax is sometimes regarded as arcane by programmers familiar with more recently developed languages such as C. Fortran has stayed abreast of such advances, however, and contemporary versions have attempted to supersede and deprecate such syntax in favor of more robust and transparent syntax. In deprecated forms of the language, it is difficult to write a lexical analyzer and one-character mistakes can lead to runtime errors rather than compilation errors. Contemporary constructs, such as free-form source, ameliorate such problems, but, as with any language, sound programming practices are the best way to avoid such errors.
One should also consider that the features of Fortran have been tuned to scientific and numerical work, as opposed to software development. For example, Fortran 95 has very short commands for performing mathematical operations on arrays which not only greatly improve program readability but also provide useful information to the compiler to enable it to vectorize operations. For these reasons, while Fortran is not often used outside scientific and engineering numerical work, it remains the language of choice for high performance numerical computing. It is also simple for non-programmers to learn how to write efficient code.
Since Fortran has been around for nearly fifty years, there is a vast body of Fortran in daily use throughout the scientific community (especially Fortran 77, the historically most important dialect). It is the primary language for some of the most intensive super-computing tasks, including weather and climate modeling.
Sample programs
The following programs can be compiled and run with any Fortran 90 or 95 compiler, such as GFortran [1]. Most modern Fortran compilers expect a file with .f95 extension.
Hello World
A simple Hello World program.
program hello print*,"Hello World!" end program hello
Cylinder Area
This program calculates the area of a cylinder. The text after the initial "!" on a line is a comment.
program cylinder !!! Calculate the area of a cylinder. !!! Declare variables and constants. !!! constants=pi !!! variables=radius squared and height implicit none ! Require all variables to be declared -- Fortran 90 feature. integer ierr character yn real radius,height,area real, parameter :: pi = 3.14159 interactive_loop: do ! Prompt the user for radius and ! height and read them. write (*,*) 'Enter radius and height.' read (*,*,iostat=ierr) radius,height ! If radius and height could not ! be read from input, then restart ! the loop. if (ierr /= 0) then write(*,*) 'Error, invalid input.' cycle interactive_loop end if ! Compute area. The ** means "raise to a power". area = 2*pi*(radius**2 + radius*height) ! Write the input variables (radius, height) ! and output (area) to the screen. write (*,'(1x,a7,f6.2,5x,a7,f6.2,5x,a5,f6.2)') & 'radius=',radius,'height=',height,'area=',area yn = ' ' yn_loop: do write(*,*) 'Perform another calculation? y[n]' read(*,'(a1)') yn if (yn=='y' .or. yn=='Y') exit yn_loop if (yn=='n' .or. yn=='N' .or. yn==' ') exit interactive_loop end do yn_loop end do interactive_loop end program cylinder
Dynamic allocation and Array Operations
This program provides an example of two features in Fortran 90: dynamic memory allocation, and array operations. Note the absence of do loops and if/then loops. Also note the use of descriptive variable names and general code formatting that comport with contemporary computer programing style. The program performs some averaging on interactively entered data.
program average !!! read in some numbers and take the average implicit none integer NumberOfPoints real, allocatable :: Points(:) real :: AveragePoints, PositiveAverage, NegativeAverage write (*,*) 'Input number of points to average:' read (*,*) NumberOfPoints allocate (Points(NumberOfPoints)) write (*,*) 'Enter the Points to average:' read (*,*) Points !!! take the average by summing Points and dividing by NumberOfPoints AveragePoints = sum(Points)/NumberOfPoints !!! now form average over positive and negative points only PositiveAverage = sum(Points, Points>0.)/count(Points>0.) NegativeAverage = sum(Points, Points<0.)/count(Points<0.) !!! print result to terminal write (*,'(''Average = '', 1g12.4)') AveragePoints write (*,'(''Average of positive points = '', 1g12.4)') PositiveAverage write (*,'(''Average of negative points = '', 1g12.4)') NegativeAverage end program average
Retro FORTRAN
A retro example of a Fortran IV (as it was called in 1968) program deck is available on the IBM 1130 page including the IBM 1130 DM2 JCL required for compilation and execution.
The standard FORTRAN joke
"GOD is REAL (unless declared INTEGER)." The joke works because, in the absence of an IMPLICIT declaration, variables beginning with the letters I through N were considered integer, and all others real.
References
General:
- Adams, Jeanne; Brainerd, Walter; Martin, Jeanne; Smith, Brian; Wagener, Jerrold (1997). Fortran 95 Handbook: Complete ISO/ANSI Reference. MIT Press.
- Metcalf, Michael; Reid, John; Cohen, Malcolm (2004). Fortran 95/2003 Explained. Oxford University Press.
- Nyhoff, Larry; Leestma, Sanford (1995). FORTRAN 77 for Engineers and Scientists with an Introduction to FORTRAN 90. 4th Edition. Prentice Hall. ISBN 013363003X.
Standards documents:
- ANSI X3.198-1992 (R1997). Title: Programming Language "Fortran" Extended. Informally known as Fortran 90. Published by ANSI.
- ISO/IEC 1539-1:1997. Title: Information technology - Programming languages - Fortran - Part 1: Base language. Informally known as Fortran 95. There are a further two parts to this standard. Part 1 has been formally adopted by ANSI.
- ISO/IEC 1539-1:2004. Title: Information technology -- Programming languages -- Fortran -- Part 1: Base language. Informally known as Fortran 2003.
External links
General
- Early Fortran Manuals
- History of FORTRAN and FORTRAN II at the Computer History Museum website
- The Fortran Company: compilers, books, tutorials, consulting
- Fortran Open Directory category
- Usenet forum: comp.lang.fortran
- Fortran 90, 95 and 2003 information
- USER NOTES ON FORTRAN PROGRAMMING (UNFP)
- Unit 7.1 FORTRAN 77 – Part of ASPIRE's textbook in Computational Science
- Unit 7.2 FORTRAN 90 – Ditto
- How Not to Write FORTRAN in Any Language There are characteristics of good coding that transcend all programming languages.
- Debugging tool for FORTRAN and its derivatives.
- Professional Programmer's Guide to Fortran77
- IBM Fortran language reference
Program repositories
- Fortran 90 Software Repository
- National HPCC software repository
- Netlib Repository
- High-Precision Software Directory
Proprietary compilers
Free software compilers
- g95 -- Fortran 95 (under development)
- Gfortran -- Fortran 95 (under development)
- Open Watcom -- Fortran 77 and C/C++
- g77 -- Fortran 77
Online Books
- Computer-Books.us - Collection of online Fortran books.
- Fortran Documentation - Fortran Documentation (particularly Fortran 77).