Fortran
Encyclopedia : F : FO : FOR : Fortran
Fortran (also FORTRAN) is a general-purposeSince FORTRAN 77, which introduced the
CHARACTER data type., proceduralSince FORTRAN II (1958)., imperative programming language that is especially suited to numeric computation and scientific computing. Originally developed in the 1950s, Fortran was used extensively in scientific and engineering applications, and is still in use in many niches such as climate modeling [link] and molecular dynamics modeling [link].Fortran (an acronym derived from its original official name, The IBM Mathematical Formula Translating System) encompasses a lineage of versions, each of which evolved to add extensions to the language while retaining compatibility with previous versions. Successive versions have added support for processing of character-based data (FORTRAN 77), array programming (Fortran 90), module-based programming (Fortran 90), object-based programming (Fortran 90), and object-oriented and generic programming (Fortran 2003).
The names of earlier versions of the language through FORTRAN 77 were conventionally spelled in all-caps (FORTRAN 77 was the last version in which the use of lowercase letters in keywords was strictly nonstandard). The capitalization has been dropped in referring to newer versions beginning with Fortran 90. The official language standards now refer to the language as "Fortran."
- 1 History
- 1.1 FORTRAN
- 1.2 FORTRAN II
- 1.3 FORTRAN III
- 1.4 FORTRAN IV
- 1.5 FORTRAN 66
- 1.6 FORTRAN 77
- 1.7 Fortran 90
- 1.8 Fortran 95
- 1.9 Fortran 2003
- 1.10 The legacy of FORTRAN
- 2 Language features
- 2.1 Fixed-form source
- 2.2 Treatment of blanks
- 2.3 Statement numbers and
GOTOstatements - 2.4 Free-form source
- 2.5 Identifiers
- 2.6 Implicit typing
- 2.7 The arithmetic
IFstatement - 2.8 More
IFstatements - 2.9 The
casestatement - 2.10 The
ENTRYstatement - 2.11 The assigned
GO TO - 3 Variants of Fortran
- 4 Criticisms and rebuttals
- 4.1 Arcane syntax
- 4.2 A language tailored to \"specialists\"
- 4.3 C/C++ is 'just as fast'
- 4.4 Fortran 95 is slower than FORTRAN 77
- 4.5 Fortran programs require static storage allocation
- 5 Code examples
- 5.1 \"Retro\" FORTRAN IV
- 5.2 Hello, world
- 5.3 Complex numbers (FORTRAN 77)
- 5.4 Calculating cylinder area (Fortran 90)
- 5.5 Dynamic memory allocation and arrays (Fortran 90)
- 5.6 Writing functions (Fortran 90)
- 5.7 Writing subroutines (Fortran 90)
- 5.8 Pointers and targets (Fortran 90)
- 5.9 Modules (Fortran 90)
- 6 FORTRAN jokes
- 7 References
- 8 Notes
- 9 External links
History
In late 1953, John W. Backus submitted a proposal to his superiors at IBM to develop a more efficient alternative to assembly language for programming their IBM 704 mainframe computer. A draft specification for The IBM Mathematical Formula Translating System was completed by mid-1954. Although originally intended for internal use only on the IBM 704 computer, the language was eventually made available to any customer purchasing an IBM 704. The first "official" manual for FORTRAN appeared in October 1956, with the first FORTRAN compiler delivered in April 1957. This was an optimizing compiler, because customers were reluctant to use a high-level programming language unless its compiler could generate code whose performance was comparable to that of hand-coded assembly language.
The language was widely adopted by scientists for writing numerically intensive programs, which encouraged compiler writers to produce compilers that could generate faster and more efficient code. The inclusion of a complex number data type in the language made Fortran especially suited to technical applications such as electrical engineering.
By 1960, versions of FORTRAN were available for the IBM 709, 650, 1620, and 7070 computers. Significantly, the increasing popularity of FORTRAN spurred competing computer manufacturers to provide FORTRAN compilers for their machines, so that by 1963 over 40 FORTRAN compilers existed. For these reasons, FORTRAN is considered to be the first, widely used cross-platform programming language#redirect [[Template:Fact]].
The development of FORTRAN paralleled the early evolution of compiler technology; indeed many advances in the theory and design of compilers were specifically motivated by the need to generate efficient code for FORTRAN programs.
FORTRAN
The initial release of FORTRAN for the IBM 704 contained 32 statements, including:
DIMENSIONandEQUIVALENCEstatements- Assignment statements
- Three-way arithmetic
IFstatement;IFstatements for checking exceptions (ACCUMULATOR OVERFLOW,QUOTIENT OVERFLOW, andDIVIDE CHECK);IFstatements for checking sense switches and sense lights. GO TO, computedGO TO,ASSIGN, assignedGO TODOloops- Formatted I/O:
FORMAT,READ,READ INPUT TAPE,WRITE,WRITE OUTPUT TAPE,PRINT,PUNCH, - Unformatted I/O:
READ TAPE,READ DRUM,WRITE TAPE,WRITE DRUM - Other I/O:
END FILE,REWIND, andBACKSPACE PAUSE,STOP, andCONTINUE,FREQUENCYstatement for optimization hints
IF statement, directly corresponded to the branch instruction on the 704 - which was also a three-way branch.FORTRAN II
IBM's FORTRAN II appeared in 1958. The main enhancement was to support procedural programming by allowing user-written subroutines and functions. Six new statements were introduced:
SUBROUTINE,FUNCTION,ENDCALL, andRETURNCOMMON
DOUBLE PRECISION and COMPLEX data types.
FORTRAN III
IBM also developed a FORTRAN III in 1958 that allowed for inline assembler code among other features; however, this version was never released as a product. Like the 704 FORTRAN and FORTRAN II, FORTRAN III included machine-dependent features that made code written in it unportable from machine to machine. Early versions of FORTRAN provided by other vendors suffered from the same disadvantage.FORTRAN IV
Starting in 1961, as a result of customer demands, IBM began development of a FORTRAN IV that removed the machine-dependent features of FORTRAN II, such asREAD INPUT TAPE, while adding new features such as a LOGICAL data type, logical Boolean expressions and the logical IF statement as an alternative to the arithmetic IF statement. FORTRAN IV was eventually released in 1962, first for the IBM 7030 ("Stretch") computer, followed by versions for the IBM 7090 and 7094.FORTRAN 66
Perhaps the most significant development in the early history of FORTRAN was the decision by the American Standards Association (now ANSI) to form a committee to develop an "American Standard Fortran." The resulting standard, ratified in March 1966, defined two languages, FORTRAN (based on FORTRAN IV, which had served as a de facto standard), and Basic FORTRAN (based on FORTRAN II, but stripped of its machine-dependent features). The FORTRAN defined by this standard became known as FORTRAN 66 (although many continued to refer to it as FORTRAN IV, the language upon which the standard was largely based). FORTRAN 66 effectively became the first "industry-standard" version of FORTRAN. FORTRAN 66 included:
- Main program,
SUBROUTINE,FUNCTION, andBLOCK DATAprogram units INTEGER,REAL,DOUBLE PRECISION,COMPLEX, andLOGICALdata typesCOMMON,DIMENSION, andEQUIVALENCEstatementsDATAstatement for specifying initial values- Intrinsic and
EXTERNAL(e.g., library) functions - Assignment statement
GOTO, assignedGOTO, and computedGOTOstatements- Logical
IFand arithmetic (3-way)IFstatements DOloopsREAD,WRITE,BACKSPACE,REWIND, andENDFILEstatements for sequential I/OFORMATstatementCALL,RETURN,PAUSE, andSTOPstatements- Hollerith constants in
DATAandFORMATstatements, and as actual arguments to procedures - Identifiers of up to 6 characters in length
- Comment lines
FORTRAN 77
After the release of the FORTRAN 66 standard, compiler vendors introduced a number of extensions to "Standard Fortran," prompting ANSI in 1969 to begin work on revising the 1966 standard. Final drafts of this revised standard circulated in 1977, leading to formal approval of the new Standard FORTRAN in April 1978. The new standard, known as FORTRAN 77, added a number of significant features to address many of the shortcomings of FORTRAN 66:
CHARACTERdata type, with vastly expanded facilities for character input and output and processing of character-based dataIMPLICIT statement- Block
IFstatement, with optionalELSEandELSE IFclauses, to provide improved language support for structured programming OPEN,CLOSE, andINQUIREstatements for improved I/O capability- Direct-access file I/O
PARAMETERstatement for specifying constantsSAVEstatement for persistent local variables- Generic names for intrinsic functions
DO WHILEandEND DOstatementsINCLUDEstatementIMPLICIT NONEvariant of theIMPLICITstatement- Bit manipulation intrinsics, based on the Industrial Real-Time Fortran (IRTF) Standard
Fortran 90
The much delayed successor to FORTRAN 77, informally known as Fortran 90, was finally released as an ANSI Standard in 1992. This major revision added many new features to reflect the significant changes in programming practice that had evolved since the 1978 standard:
- Free-form source input, also with lowercase Fortran keywords
- Modules, to group related procedures and data together
RECURSIVEprocedures- A vastly improved argument-passing mechanism, allowing interfaces to be checked at compile time
- User-written interfaces for generic procedures
- Operator overloading
- Derived/abstract data types
- New data type declaration syntax, to specify the data type and other attributes of variables
- Ability to operate on arrays as a whole in expressions and assignment statements, thus greatly simplifying math and engineering computations
ALLOCATABLEattribute, andALLOCATEandDEALLOCATEstatements, for dynamic memory allocationPOINTERattribute, pointer assignment, andNULLIFYstatement to facilitate the creation and manipulation of dynamic data structuresCASEconstruct for multi-way selectionEXITandCYCLEstatements for "breaking out" of normalDOloop iterations in an orderly way- Identifiers up to 31 characters in length
- Inline comments
- Portable specification of numerical precision under the user's control
Fortran 95
Fortran 95 was a minor revision, mostly to resolve some outstanding issues from the Fortran 90 standard. Nevertheless, Fortran 95 also added a number of extensions, notably from the High Performance Fortran dialect:
FOR ALLand nestedWHEREconstructs to aid vectorization- User-defined
PUREandELEMENTALprocedures
ALLOCATABLE arrays, prior to the availability of fully Fortran 2003-compliant Fortran compilers. Such uses include ALLOCATABLE arrays as derived type components, in procedure dummy argument lists, and as function return values. ALLOCATABLE arrays are preferable to POINTER-based arrays because ALLOCATABLE arrays are required by Fortran 95 to be automatically deallocated when they go out of scope, eliminating the possibility of memory leakage. In addition, aliasing is not an issue for optimization of array references, allowing compilers to generate faster code than in the case of pointers.Fortran 2003
The most recent standard, Fortran 2003, is a major revision introducing a number of extensions:
- Object-oriented programming
- Floating-point exception handling
- Built-in language support for IEEE floating-point arithmetic
- Improved interoperability with the C language
gzipped PostScript file].The legacy of FORTRAN
Since Fortran has been around for nearly fifty years, there is a vast body of Fortran in daily use throughout the scientific and engineering communities. It is the primary language for some of the most intensive supercomputing tasks, such as weather/climate modeling, computational chemistry, quantum chromodynamics and simulation of automobile crash dynamics. Indeed, one finds that even today, half a century later, floating-point benchmarks to gauge the performance of new computer processors are still written in Fortran (e.g., [CFP2000], the floating-point component of the SPEC [CPU2000] benchmarks), an eloquent tribute to the longevity of the language.Language features
The following subsections are not intended to be a comprehensive overview of the Fortran language; that role properly belongs to any of the excellent Fortran textbooks listed at the end of this article. Rather, the following describe some of the more salient features of the language, particularly those peculiar to Fortran. Although both old and new features are described, the emphasis here is on historic features, many of which are rarely used in modern programs. Still, most have been retained in the language to maintain compatibility with old code.Fixed-form source
Early FORTRAN programs were handwritten on coding forms specially designed for the purpose or on plain paper and then punched on punch cards. Each punch card had 80 columns, numbered 1–80, and represented a single line of source input. Formatting rules were rigid:
- If a
C(or asterisk, as permitted by FORTRAN 77) was punched in column 1, the card was a comment line, ignored by the compiler - A numeric statement label (in the range
1–99999) could be punched in columns 1–5 if needed - If any (non-blank) character other than zero was punched in column 6, the card was taken to be a continuation of the previous statement. The maximum number of continuation lines was standardized to 19 with FORTRAN 77
- The contents of columns 7–72 were treated as a statement (or a continuation of the previous statement in the case of a continuation card)
- Columns 73–80 were completely ignored by the compiler. These columns were often used for sequence numbers, as a precaution in case the card deck should get dropped or out of order. (With programs consisting of hundreds to thousands of lines, this was a serious issue.) Some source code tools would use also these columns to record modification history.
Treatment of blanks
Blanks were, in general, not significant in fixed-form FORTRAN programs. For example, theGOTO statement could equivalently be (and very often was) punched as GO TO. Likewise, a statement such asWith FORTRAN 77, it became standard practice to insert a comma between the first statement number and the DO variable; however, this comma could be omitted for compatibility with FORTRAN 66.
DO 1 I = 1, 10could equivalently be punched as:
DO1I=1,10Minor typos could lead to wildly different semantics; for example, if the comma above were inadvertently punched as a period, the above statement (intended to implement a
DO loop) would be interpreted as the assignment statement
DO1I = 1.10This would not be flagged by the compiler as an error, due to the implicit typing mechanism (see below) which allows variables to be declared by their first use.
In addition, in order to save the keypunch operator the trouble of punching lots of zeroes in input data cards, blanks in numeric data fields were, depending on the context and on the particular compiler, interpreted by FORTRAN input statements as zeroes.
As part of the Fortran 90 free-form source form, blank significance is now required. The older rules were retained for the now-deprecated fixed-form source. Also as of Fortran 90, implicit typing can be disabled.
Statement numbers and GOTO statements
Early FORTRAN programs (prior to Fortran 90) relied heavily on statement numbers and the GOTO statement to implement control structures. For example, the number immediately following the DO keyword above specified the statement number of the last statement enclosed in the body of the DO loop.
Using the IF statement combined with the GOTO statement, it was possible to implement looping constructs such as a while (condition) do ... construct (with the test at the top, illustrated here using the block IF statement introduced with FORTRAN 77):
1 IF (condition) THEN executable_statements GOTO 1 END IFas well as a repeat ... until (condition) construct (with the test at the bottom, illustrated here using the logical
IF statement introduced with FORTRAN 66):
1 executable_statements IF (.NOT.condition) GOTO 1Statement numbers were also required as targets to handle I/O exceptions, such as those handled by the
END= and ERR= specifiers in I/O statements such as READ and WRITE, among others.Free-form source
Fortran 90 introduced free-form source input, which removed almost all the column-based restrictions on the input layout discussed above. Lines could be as many as 132 characters long. Blanks were now significant as token delimiters. Statements could be continued on following lines by ending incomplete lines with&.As of Fortran 95, the older fixed-form source has been considered deprecated.
Identifiers
Names of identifiers in early compilers were usually limited to the number of alphanumeric characters that fit in one machine word; six characters was typical (the original IBM 704 used 36-bit machine words, and alphanumeric characters were encoded in 6-bit binary-coded decimal, packed six characters per word). However, the maximum could be as few as three characters, depending on the machine.Both the FORTRAN 66 and FORTRAN 77 standards required compilers to support at least six characters for identifiers, though many compilers supported longer identifiers. Fortran 90 guaranteed support for identifiers of up to 31 characters, while Fortran 2003 extended this support to 63 characters.
Implicit typing
By default, numeric variables did not need to be declared before use; their declaration was implicit with their first appearance in a program. Furthermore, variables whose names began with any of the letters fromI through N were implicitly assumed to be INTEGER variables, whereas all other variables were assumed to be REALs. (In mathematics, the symbols i through n are typically used as (integer) subscripts and superscripts.) This implicit typing mechanism could be overridden using an IMPLICIT statement to change the default types associated with variables based on their initial letter.
Alternatively, explicit variable declarations could be used to declare the types of variables. Explicit declarations were also necessary for variables of type other than REAL and INTEGER (namely for the DOUBLE PRECISION, COMPLEX, LOGICAL, and CHARACTER data types).
Because simply mistyping a variable name effectively caused a new variable to be declared according to the implicit typing rules above (assuming the mistyped name was not previously declared), later compilers provided an IMPLICIT NONE statement to allow turning off the implicit typing mechanism, forcing all variables to have to be declared before use. The IMPLICIT NONE statement became standard with Fortran 90. Most Fortran coding standards mandate the use of IMPLICIT NONE.
The arithmetic IF statement
In the earliest versions of Fortran, the only conditional branching statement provided was the three-way arithmetic IF statement:
IF (expression) n1, n2, n3The conditional expression was an INTEGER- or REAL-valued numeric expression, and control would be passed to statement number n1, n2, or n3 depending on whether the expression was negative, zero, or positive, respectively.Note that in the case of a REAL-valued expression, the usual caveat regarding tests for floating-point equality applies, i.e., an expression mathematically expected to evaluate to zero may not exactly equal zero due to round-off error and the limited internal precision of floating-point arithmetic.
Although seemingly arcane, the arithmetic IF statement was convenient shorthand for branching based on the three possible conditions of [x < y], [x = y], or [x > y], in the case where the test expression was of the form X - Y. Thus, one was often taught mentally to view a statement of the form
IF (X - Y) 100, 200, 300as FORTRAN idiom for the following group of equivalent statements (here, expressed as logical
IF statements, which were not introduced until FORTRAN 66):
IF (X .LT. Y) GO TO 100 IF (X .EQ. Y) GO TO 200 IF (X .GT. Y) GO TO 300Obviously, in the case of "less-than-or-equal-to" or "greater-than-or-equal-to," the first two or last two, respectively, of the
GOTO targets would be the same.This feature is described for historic reasons, as it is rarely, if ever, used in modern Fortran
More IF statements
As alluded to previously, FORTRAN 66 codified the logical IF statement. This allowed most Fortran statements, including the GO TO statement, to be conditionally executed:
IF (expression) executable_statementFORTRAN 77 introduced the block
IF statement with optional ELSE IF and ELSE clauses to support structured programming:
IF (expression) THEN executable_statements ELSE IF (expression) THEN executable_statements ELSE executable_statements END IF
The case statement
Fortran 90 introduced the case selection structure to implement multi-way branches. The following artificial example mimics the arithmetic IF statement above (but only in the case of an integer-valued test expression, as values such as [-0.5] or [+0.5] would fall through the cracks in this example):
select case(i - j) case(:-1) ! Former code jumped to by GOTO 100 case(0) ! Former code jumped to by GOTO 200 case(1:) ! Former code jumped to by GOTO 300 end select
The ENTRY statement
The earliest versions of FORTRAN were developed before modern recommended practices on topics such as control flow had matured. One example is the ENTRY statement, which allowed a program to jump to an arbitrary point within a subprogram, violating the single-entry control flow philosophy of modern programming practice. In the example below, FUN is a function expecting three variable arguments, while FUNALT is an alternate entry point invoked with only two arguments; for illustration, the third argument is given a default value for the alternate entry point:
FUNCTION FUN(A, B, OPTARG) statements specific to FUN GOTO 1 ENTRY FUNALT(A, B) OPTARG = 0 statements specific to FUNALT 1 statements common to both RETURN ENDEqually as interesting, FORTRAN also supports alternate
RETURN, where a subprogram can specify an alternate point to which to return, rather than the original point at which the subprogram was invoked.
As of Fortran 95, both ENTRY and alternate RETURN are now considered deprecated features.
The assigned GO TO
A variable may be assigned a statement label (typically implemented as a code pointer) for subsequent use as a location either to continue execution or as a format statement.
INTEGER nextstmt ASSIGN 45 TO nextstmt 10 GO TO nextstmt 15 ASSIGN 90 TO nextstmt GO TO 10 25 ASSIGN 15 TO nextstmt GO TO 10 35 ASSIGN 25 TO nextstmt GO TO 10 45 ASSIGN 35 TO nextstmt GO TO 10 90 CONTINUEThis silly example jumps to 45 (after the first assign), which reassigns the variable and jumps to 10 to execute the indirect jump to 35, and so on until statement 15 breaks out to the continue at statement 90. Since Fortran 95, implementions no longer recognize this feature. [IBM Fortran manual for GO TO (assigned)]
More usefully, FORMAT statements may be assigned
SUBROUTINE occurences (N)
INTEGER fmt
10 FORMAT ('No occurrences found')
11 FORMAT ('One occurrence found')
12 FORMAT (I4, ' occurrences found')
IF (N-1) 20, 21, 22 ! if N<1, goto 20; if N=1, goto 21; if N>1, goto 22
20 ASSIGN 10 to fmt
GOTO 30
21 ASSIGN 11 to fmt
GOTO 30
22 ASSIGN 12 to fmt
30 WRITE (6, fmt) N
END SUBROUTINE
Many implementations did not protect against invalid assignments or use of assigned variables, even allowing arithmetic.
SUBROUTINE crash4ways INTEGER dest GOTO dest ! jumps to random location pointed to by uninitialized variable dest10 FORMAT ('message') ASSIGN 10 TO dest GOTO dest ! jumps to data
20 CONTINUE ASSIGN 20 TO dest PRINT dest ! uses machine code at label 20 as format specification; ! almost certain to give runtime error "invalid format"
30 ASSIGN 30 TO dest dest = dest * 3 GOTO dest ! a random jump likely to fault on illegal instructions or data
Variants of Fortran
Specific variants
Vendors of high-performance scientific computers (e.g., Burroughs, CDC, Cray, Honeywell, IBM, Texas Instruments, and UNIVAC) added extensions to Fortran to take advantage of special hardware features such as instruction cache, CPU pipelines, and vector arrays. For example, one of IBM's Fortran compilers (H Extended IUP) had a level of optimization which reordered the machine language instructions to keep multiple internal arithmetic units busy simultaneously. Another example is CFD, a special variant of Fortran designed specifically for the ILLIAC IV supercomputer, running at NASA's Ames Research Center.Such machine-specific extensions have either disappeared over time or have had elements incorporated into the main standards; the major remaining extension is OpenMP, which is a cross-platform extension for shared memory programming. One new extension, CoArray Fortran, is intended to support parallel programming.
Fortran-based languages
Prior to FORTRAN 77, a number of preprocessors were commonly used to provide a friendlier language, while retaining the advantage that the preprocessed code could still be compiled on any machine with a standard FORTRAN compiler. Popular preprocessors included FLECS, MORTRAN, ratfor, and ratfiv. (Ratfor and ratfiv, for example, implemented a remarkably C-like language, outputting preprocessed code in standard FORTRAN 66.)Among the more popular specialized Fortran-based languages that evolved over time are SAS, for generating statistical reports, and SIMSCRIPT, for modeling and simulating large discrete systems.
F was designed to be a clean subset of Fortran 95 that attempted to remove the redundant, unstructured, and deprecated features of Fortran, such as the EQUIVALENCE statement.
Criticisms and rebuttals
Arcane syntax
As what was essentially a first attempt at designing a high-level language, Fortran's syntax is sometimes regarded as archaic by programmers familiar with subsequently 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.Early FORTRAN syntax (prior to Fortran 90) did not in general treat blanks as significant, which made writing robust and efficient lexical analyzers for Fortran difficult. Subsequent developments such as free-form source have largely resolved these issues.
A language tailored to \"specialists\"
Fortran can rightly be criticized for issues such as its archaic syntax, arbitrary conventions such as implicit typing and fixed-form source input, and support for bad programming practices. However, 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 provides concise statements for applying mathematical operations directly to entire arrays, which not only improve program readability but also assist the compiler in vectorizing 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. For example, Fortran supports COMPLEX variables used for Fourier transforms among other applications, while C does not. Fortran is also simple for non-programmers, particularly those with a mathematical or technical background, to learn how to write efficient, numerically robust code.C/C++ is 'just as fast'
Fortran was designed to create fast executable code for scientific programming. One reason for this attitude was that it was intended to compete against hand-written assembly language from the start. In scientific computing, this means that efficient array processing is of critical concern. Many other languages do not treat arrays, especially multi-dimensional arrays, as cleanly and simply as Fortran does.
In particular, Fortran data items are not allowed to alias one another, except in very well-defined and explicit situations. This allows a Fortran compiler much flexibility to generate very efficient code, by default, compared to pointer-intensive languages. Explicit use of certain C features, such as the register keyword and the C99 restrict keyword can mitigate some of the performance problems of C.
Fortran 95 is slower than FORTRAN 77
Most programs in FORTRAN 77 are valid in Fortran 95, or require only minor changes. The new Fortran features often allow the programmer to express his intentions more clearly and consistently, allowing easier and better optimization. A slowdown when rewriting programs from FORTRAN 77 to Fortran 95 is usually caused by improper usage of Fortran 95 features.Fortran programs require static storage allocation
It is a common misconception that Fortran requires static storage allocation for all local variables. Both FORTRAN 66 and FORTRAN 77 Standards were written to merely allow a fully static implementation. FORTRAN 77 introduced theSAVE statement to allow a programmer to explicitly state which variables are required to retain their values when they go out of scope. Likewise, Fortran 90 introduced RECURSIVE procedures. In a recursive procedure, non-SAVEd local variables must be unique for each active instance of the procedure.Code examples
The following sample programs can be compiled and run with any standard Fortran compiler (see the end of the article for lists of open-source and proprietary compilers). Most modern Fortran compilers expect a file with a.f extension (for FORTRAN 66 or FORTRAN 77 source, although the FORTRAN 66 dialect may have to be selected specifically with a command-line option) or .f90/.f95 extension (for Fortran 90/95 source, respectively).\"Retro\" FORTRAN IV
A retro example of a FORTRAN IV (later evolved into FORTRAN 66) program deck is available on the IBM 1130 page, including the IBM 1130 DM2 JCL required for compilation and execution. The IBM 1130 emulator (available at [IBM 1130.org]) will allow the FORTRAN IV program to be complied and run on a PC.The Snoopy Calendar program is the classic Fortran program referenced in many nostalgic items on computer history (eg. Real Programmers Don't Use Pascal). This version can also be run on the IBM 1130 emulator.
Hello, world
In keeping with long-standing computing tradition, the first example presented is a simple program to display the words "Hello, world" on the screen (or printer).FORTRAN 66
In FORTRAN 66 (also FORTRAN IV):
C FORTRAN IV WAS ONE OF THE FIRST PROGRAMMING C LANGUAGES TO SUPPORT SOURCE COMMENTS WRITE (6,7) 7 FORMAT(12HHELLO, WORLD) STOP ENDThis program prints "HELLO, WORLD" to Fortran unit number 6, which on most machines was the line printer or terminal. (The card reader or keyboard was usually connected as unit 5). The number 7 in the
WRITE statement refers to the statement number of the corresponding FORMAT statement. FORMAT statements may be placed anywhere in the same program or function/subroutine block as the WRITE statements which reference them. Typically a FORMAT statement is placed immediately following the WRITE statement which invokes it; alternatively, FORMAT statements are grouped together at the end of the program or subprogram block. If execution flows into a FORMAT statement, it is a no-op. So the example has only two executable statements: WRITE and STOP.
The initial 12H in the FORMAT statement in the above example defines a Hollerith constant, here meaning that the 12 characters immediately following are to be taken as a character constant (note that the Hollerith constant is not surrounded by delimiters). (Some compilers also supported character literals enclosed in single quotes, a practice that came to be standard with FORTRAN 77.)
FORTRAN 77
As of FORTRAN 77, single quotes are used to delimit character literals, and inline character strings may be used instead of references toFORMAT statements. Comment lines could be indicated with either a C or an asterisk in column 1.
PROGRAM HELLOW * The PRINT statement is like WRITE, * but prints to the standard output unit PRINT '(A)', 'Hello, world' STOP END
Fortran 90
As of Fortran 90, double quotes are allowed in addition to single quotes. An updated version of the Hello, world example (which makes use of list-directed I/O, supported as of FORTRAN 77) could be written in Fortran 90 as follows:
program HelloWorld write (*,*) 'Hello, world!' ! This is an inline comment end program HelloWorld
Complex numbers (FORTRAN 77)
The following FORTRAN 77 example prints out the values of [e^] (where [j = \sqrt]) for values of [i = 0, 1, \ldots, 7].- cmplxd.f (FORTRAN 77)
- Demonstration of COMPLEX numbers
- Prints the values of e ** (j * i * pi / 4) for i = 0, 1, 2, ..., 7
- where j is the imaginary number sqrt(-1)
PROGRAM CMPLXD IMPLICIT COMPLEX(X) PARAMETER (PI = 3.141592653589793, XJ = (0, 1)) DO 1, I = 0, 7 X = EXP(XJ * I * PI / 4) IF (AIMAG(X).LT.0) THEN PRINT 2, 'e**(j*', I, '*pi/4) = ', REAL(X), ' - j',-AIMAG(X) ELSE PRINT 2, 'e**(j*', I, '*pi/4) = ', REAL(X), ' + j', AIMAG(X) END IF 2 FORMAT (A, I1, A, F10.7, A, F9.7) 1 CONTINUE STOP ENDThe above example is intended to illustrate the following:
- The
IMPLICITstatement can be used to specify the implicit type of variables based on their initial letter if different from the default implicit typing scheme described above. In this example, this statement specifies that the implicit type of variables beginning with the letterXshall beCOMPLEX. - The
PARAMETERstatement may be used to specify constants. The second constant in this example (XJ) is given the complex-valued value [0 + j1], where [j] is the imaginary number [\sqrt]. - The first number in the
DOstatement specifies the number of the last statement considered to be within the body of theDOloop. In this example, as neither theEND IFnor theFORMATis a single executable statement, theCONTINUEstatement (which does nothing) is used simply in order for there to be some statement to denote as the final statement of the loop. EXP()corresponds to the exponential function [e^x]. In FORTRAN 77, this is a generic function, meaning that it accepts arguments of multiple types (such asREALand, in this example,COMPLEX). In FORTRAN 66, a specific function would have to be called by name depending on the type of the function arguments (for this example,CEXP()for aCOMPLEX-valued argument).- When applied to a
COMPLEX-valued argument,REAL()andAIMAG()return the values of the argument's real and imaginary components, respectively. - Multiple input/output statements may share the same
FORMATstatement.
mac:~/Desktop $ cmplxd e**(j*0*pi/4) = 1.0000000 + j0.0000000 e**(j*1*pi/4) = 0.7071068 + j0.7071068 e**(j*2*pi/4) = 0.0000000 + j1.0000000 e**(j*3*pi/4) = -0.7071068 + j0.7071068 e**(j*4*pi/4) = -1.0000000 - j0.0000001 e**(j*5*pi/4) = -0.7071066 - j0.7071069 e**(j*6*pi/4) = 0.0000000 - j1.0000000 e**(j*7*pi/4) = 0.7071070 - j0.7071065Error can be seen occurring in the last decimal place in some of the numbers above, a result of the
COMPLEX data type representing its real and imaginary components in single precision. Incidentally, Fortran 90 also introduced a double-precision complex-number data type.Calculating cylinder area (Fortran 90)
The following program, which calculates the surface area of a cylinder, illustrates free-form source input and other features introduced by Fortran 90.
program cylinder! Calculate the surface area of a cylinder. ! ! Declare variables and constants. ! constants=pi ! variables=radius squared and height
implicit none ! Require all variables to be explicitly declaredinteger :: ierr character :: yn real :: radius, height, area real, parameter :: pi = 3.1415926536interactive_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 cycle through 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=',areayn = ' ' 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_loopend do interactive_loopend program cylinder
Dynamic memory allocation and arrays (Fortran 90)
The following program illustrates dynamic memory allocation and array-based operations, two features introduced with Fortran 90. Particularly noteworthy is the absence ofDO loops and IF/THEN statements in manipulating the array; mathematical operations are applied to the array as a whole. Also apparent is the use of descriptive variable names and general code formatting that comport with contemporary programming style. This example computes an average over data entered interactively.
program average! read in some numbers and take the average ! As written, if there are no data points (or no positive/negative points) ! an average of zero is returned. ! While this may not be desired behavior, it keeps this example simple.
implicit none integer :: NumberOfPoints real, dimension(:), allocatable :: Points real :: AveragePoints=0., PositiveAverage=0., NegativeAverage=0.write (*,*) 'Input number of points to average:' read (*,*) NumberOfPointsallocate (Points(NumberOfPoints))write (*,*) 'Enter the Points to average:' read (*,*) Points! take the average by summing Points and dividing by NumberOfPointsif (NumberOfPoints>0) AveragePoints = sum(Points)/NumberOfPoints! now form average over positive and negative points onlyif (count(Points>0.)>0) PositiveAverage = sum(Points, Points>0.)/count(Points>0.) if (count(Points<0.)>0) NegativeAverage = sum(Points, Points<0.)/count(Points<0.)deallocate (Points)! print result to terminalwrite (*,'(Average = , 1g12.4)') AveragePoints write (*,'(Average of positive points = , 1g12.4)') PositiveAverage write (*,'(Average of negative points = , 1g12.4)') NegativeAverageend program average
Writing functions (Fortran 90)
Modern Fortran features available for use with procedures, including deferred-shape, protected, and optional arguments, are illustrated in the following example, a function to solve a system of linear equations.
function GaussSparse(NumIter, Tol, b, A, X, ActualIter)! This function solves a system of equations (Ax = b) by using the Gauss-Jordan Method
implicit nonereal GaussSparse! Input: its value cannot be modified from within the functioninteger, intent(in) :: NumIter real, intent(in) :: Tol real, intent(in), dimension(1:) :: b real, intent(in), dimension(1:,1:) :: A! Input/Output: its input value is used within the function, and can be modifiedreal, intent(inout), dimension(1:) :: X! Output: its value is modified from within the function, only if the argument is requiredinteger, optional, intent(out) :: ActualIter! Localsinteger i, n, Iter real TolMax, Xk! Initialize valuesn = ubound(b, dim = 1) ! Size of array, obtained using ubound intrinsic function TolMax = 2. * Tol Iter = 0! Compute solution until convergenceconvergence_loop: do while (TolMax >= Tol.AND.Iter < NumIter); Iter = Iter + 1TolMax = -1. ! Reset the tolerance value! Compute solution for the k-th iterationiteration_loop: do i = 1, n! Compute the current x-valueXk = (b(i) - sum(A(i,1:i-1) * X(1:i-1)) - sum(A(i,i+1:n) * X(i+1:n))) / A(i, i)! Compute the error of the solutionTolMax = max((abs(X(i) - Xk)/(1. + abs(Xk))) ** 2, abs(A(i, i) * (X(i) - Xk)), TolMax) X(i) = Xk enddo iteration_loop enddo convergence_loopif (present(ActualIter)) ActualIter = Iter GaussSparse = TolMaxend function GaussSparse
Note that an explicit interface to this routine must be available to its caller so that the type signature is known. This is preferably done by placing the function in a MODULE and then USEing the module in the calling routine. An alternative is to use an INTERFACE block.
Writing subroutines (Fortran 90)
In those cases where it is desired to return values via a procedure's arguments, a subroutine is preferred over a function; this is illustrated by the following subroutine to swap the contents of two arrays (this example assumes that the arrays are of equal size):
subroutine Swap_Real(a1, a2)
implicit none! Input/Outputreal, intent(inout) :: a1(:), a2(:)! Localsinteger :: lb(1), & !Lower bound ub(1) !Upper bound integer i real a! Get boundslb = lbound(a1) ub = ubound(a1)! Swapdo i = lb(1), ub(1) a = a1(i) a1(i) = a2(i) a2(i) = a enddoend subroutine Swap_Real
As in the previous example, an explicit interface to this routine must be available to its caller so that the type signature is known. As before, this is preferably done by placing the function in a MODULE and then USEing the module in the calling routine. An alternative is to use a INTERFACE block.
Pointers and targets (Fortran 90)
In Fortran, the concept of pointers differs from that in C-like languages. A Fortran 90 pointer does not merely store the memory address of a target variable; it also contains additional descriptive information such as the target's rank, the upper and lower bounds of each dimension, and even strides through memory. This allows a Fortran 90 pointer to point at submatrices.
Fortran 90 pointers are "associated" with well-defined "target" variables, via either the pointer assignment operator (=>) or an ALLOCATE statement. When appearing in expressions, pointers are always dereferenced; no "pointer arithmetic" is possible.
The following example illustrates the concept:
program Test! NOTE: Variable expressions in format statements (e.g.,
or ) ! are compiler-dependent. ! Also, array notation (e.g., [1,2,3]) is a Fortran 2003 feature; ! for Fortran 95, use (/1,2,3/) instead.
use FunctionsModule, only: DoSomething => A !This function performs some !operation on the integer !input and returns its result implicit noneinteger, parameter :: m = 3, n = 3 integer, pointer :: p(:)=>null(), q(:,:)=>null() integer, allocatable, target :: A(:,:) integer ios = 0allocate(A(1:m, 1:n), q(1:m, 1:n), stat = ios) if (ios /= 0) stop 'Error during allocation of A and q'! Assign the matrix ! A = [[1 4 7] ! [2 5 8] ! [3 6 9]]A = reshape([(i, i = 1, m*n)], [m, n]) q = A! p will be associated with the first column of Ap => A(:, 1)! This operation on p has a direct effect on matrix Ap = p ** 2! This will end the association between p and the first column of Anullify(p)! Matrix A becomes: ! A = [[1 4 7 ] ! [4 5 8 ] ! [9 6 9 ]]write(*, '(/, "Matrix A becomes:",/,"A = [",! Perform some array operation("[", (i1,2x),"]",/,5x)"]")') & ((A(i, j), j = 1, n), i = 1, m) q = q + A! Matrix q becomes: ! q = [[ 2 8 14 ] ! [ 6 10 16 ] ! [12 12 18 ]]write(*, '(/, "Matrix q becomes:",/,"q = [",! Use p as an ordinary array("[", (i2,2x),"]",/,5x)"]")') & ((q(i, j), j = 1, n), i = 1, m) allocate (p(1:m*n), stat = ios) if (ios /= 0) stop 'Error during allocation of p'! Perform some array operationp = [((DoSomething(A(i, j) + A(i, j) ** 2), i = 1, m), j = 1, n)]write(*, '((i1,4x,"p[",i1,"] = ",i5))') (i, p(i), i = 1, m * n) deallocate(A, p, q, stat = ios) if (ios /= 0) stop 'Error during deallocation'end program Test
Modules (Fortran 90)
A module is a program unit which contains data definitions, global data, andCONTAINed procedures. Unlike a simple INCLUDE file, a module is an independent program unit that can be compiled separately and linked in its binary form. Once compiled, a module's public contents can be made visible to a calling routine via the USE statement.
The module mechanism makes the explicit interface of procedures easily available to calling routines. In fact, modern Fortran encourages every SUBROUTINE and FUNCTION to be CONTAINed in a MODULE. This allows the programmer to use the newer argument passing options, and allows the compiler to perform full type checking on the interface.
The following example also illustrates derived types and overloading of procedures and operators.
module GlobalModule! Reference to a pair of procedures included in a previously compiled ! module named PortabilityLibrary
use PortabilityLibrary, only: GetLastError, & ! Generic procedure Date ! Specific procedure! Constantsinteger, parameter :: dp_k = kind (1.0d0) ! Double precision kind real, parameter :: zero = (0.) real(dp_k), parameter :: pi = 3.14159265358979_dp_k! Variablesinteger :: n, m, retint logical :: status, retlog character(50) :: AppName! Arraysreal, allocatable, dimension(:,:,:) :: a, b, c, d complex(dp_k), allocatable, dimension(:) :: z! Derived type definitionstype ijk integer i integer j integer k end type ijktype matrix integer m, n real, allocatable :: a(:,:) ! Fortran 2003 feature. For Fortran 95, ! use the pointer attribute instead end type matrix! All the variables and procedures from this module can be accessed ! by other program units, except for AppNamepublic private AppName! Procedure overloadinginterface swap module procedure swap_Integer, swap_Real end interface swapinterface GetLastError ! This adds a new, additional procedure to the ! generic procedure GetLastError module procedure GetLastError_GlobalModule end interface GetLastError! Operator overloadinginterface operator(+) module procedure add_ijk end interface! Prototype for external procedureinterface real function GaussSparse(NumIter, Tol, b, A, X) integer, intent(in) :: NumIter real, intent(in) :: Tol real, intent(in), dimension(1:) :: b real, intent(in), dimension(1:,1:) :: A real, intent(inout), dimension(1:) :: X end function GaussSparse end interface! Procedures included in the modulecontains! Internal functionfunction add_ijk(ijk_1, ijk_2) type(ijk) add_ijk, ijk_1, ijk_2 intent(in) :: ijk_1, ijk_2 add_ijk = ijk(ijk_1%i + ijk_2%i, ijk_1%j + ijk_2%j, ijk_1%k + ijk_2%k) end function add_ijk! Include external filesinclude 'Swap_Integer.f90' ! Comments SHOULDN'T be added on include lines include 'Swap_Real.f90'end module GlobalModule
FORTRAN jokes
For a programming language with a half-century legacy, FORTRAN not surprisingly has accumulated its share of jokes and folklore.Standard jokes
- "GOD is REAL unless declared INTEGER."
- Joke, circa 1980 (following the standardization of FORTRAN 77): "Q: What will the scientific programming language of the year 2000 look like? ... A: Nobody knows, but its name will be FORTRAN."
From the historical record
- "The primary purpose of the DATA statement is to give names to constants; instead of referring to pi as 3.141592653589793 at every appearance, the variable PI can be given that value with a DATA statement and used instead of the longer form of the constant. This also simplifies modifying the program, should the value of pi change." —Early FORTRAN manual for Xerox ComputersPrior to FORTRAN 77, which introduced the
PARAMETERstatement (the preferred method of defining constants), theDATAstatement was the conventional way to define constants, although this statement merely specified initial values and did not prevent the corresponding variables from being modified (as by regular assignment statements). It should be noted that the "best" value of pi in a program does indeed change if one moves the program to a computer with a different number of bits per word. For that reason, it is recommended to assign PI as PI = ACOS(-1.) or PI = 4.*ATAN(1.), which are portable ways of writing PI to maximum precision. (There is no round-off error when multiplying a REAL by 4 because it simply increases the power of 2 exponent for the number.)
- "Consistently separating words by spaces became a general custom about the tenth century A. D., and lasted until about 1957, when FORTRAN abandoned the practice." —Sun FORTRAN Reference Manual
References in popular culture
- In the pilot episode of the Futurama series, the robot Bender drinks a bottle of "Olde FORTRAN Malt Liquor" (alluding to "Olde English" malt liquor)
References
Textbooks
\"Core\" language standards
Related standards
Notes
External links
History
- [Historic Documents in Computer Science] — Early Fortran manuals
- * [The FORTRAN Automatic Coding System for the IBM 704 EDPM] (PDF file) — The very first Fortran manual, by John Backus et al. and dated "15 October 1956" (Attests to the origin of the name FORTRAN as deriving from The IBM Mathematical Formula Translating System. And note that the front cover page refers to the language as Fortran, not FORTRAN.)
- [History of FORTRAN and FORTRAN II] at the [Computer History Museum]
Standards
- [JTC1/SC22/WG5] — The ISO/IEC Fortran Working Group
Tutorials
- [Unit 7.1 FORTRAN 77] — Part of [ASPIRE]'s "Computational Science Textbook" project
- [Unit 7.2 Fortran 90] — Part of [ASPIRE]'s "Computational Science Textbook" project
- [''User Notes on FORTRAN Programming (UNFP)] — "An open cooperative practical guide"
References
- [Programming:Fortran overview] from Wikibooks
- [IBM XL Fortran 6.1 Language Reference]
- [Fortran 90, 95, 2003, 77 Information Resources] at Information Services and Systems, [King's College London]
- [Fortran 77] — FORTRAN 77 documentation
Code repositories
- [FLIB2C (Fortran Library to C)] at [MYCPLUS] — "A tool to create header files that provide a simple and easy-to-use interface between FORTRAN (library) routines and C"
- [Fortran 90 Software Repository] at [NAG] (Numerical Algorithms Group)
- [High-Precision Software Directory] at the [Computational Research Division] of [Lawrence Berkeley National Laboratory]
- [National HPCC Software Exchange] at the [Department of Computer Science] at the [University of Tennessee at Knoxville]
- [Netlib Repository] at the [University of Tennessee at Knoxville] and [Oak Ridge National Laboratory]
Free (open-source) compilers
- [g77]: "g77 is a free Fortran 77 compiler...now integrated into the GNU Compiler Collection (GCC)." Note: gcc, as of version 4.0, now includes GFortran as the default Fortran compiler in lieu of g77
- [g95]: "The goal of the g95 project is to create a free, open source Fortran 95 compiler and runtime libraries"
- [Gfortran]: "The GNU Fortran 95 compiler, part of GCC"
- [High Performance Computing for Mac OS X] — Fortran compilers for Mac OS X
- [Open Watcom]: "A joint effort between SciTech Software Inc, Sybase and the Open Source development community to maintain and enhance the Watcom C/C++ and Fortran cross compilers and tools"
Proprietary compilers
- [Absoft Corporation]
- [Cray Inc]
- [Fujitsu]
- [HP]
- [IBM]
- [Intel]
- [Lahey]
- [NAG]
- [PathScale]
- [Portland Group]
- [Salford]
- [SGI]
- [Sun]
Free graphical libraries/GUI
- [f90gl]: "f90gl is a public domain implementation of the official Fortran 90 bindings for OpenGL." For Linux, Mac OS X, Solaris, UNIX, Windows. Public domain.
- [ftcl: Combining Fortran and Tcl in one program] — A Fortran–Tcl/TK interface. Open-source.
- [g2 graphical library]: "g2 is an easy to use, portable and powerful 2D graphics library." For Linux, Mac OS X, OpenVMS, Solaris, UNIX, Windows. GNU LGPL.
- [GrWin Graphics Library]: "GrWin Graphics Library is a free graphics routine library for Fortran and C/C++." For Windows. Open-source.
- [japi (Java Application Programming Interface)]: "An open source free software GUI toolkit." For Linux, Mac OS X, Solaris, UNIX, Windows. GNU LGPL.
- [Platform Independent Library for Fortran 90/95]: "The portable way [for] creating GUIs, doing file IO etc. This library is an attempt to build a platform and compiler independent library for Fortran 9x." For Linux, Mac OS X, UNIX, Windows. GNU LGPL.
- [PLplot – a Scientific Plotting Library]: "PLplot is a library of functions that are useful for making scientific plots." For Linux, Mac OS X, MS-DOS, Unix, Windows. GNU LGPL.
- [Xeffort]: "Xeffort is a free, open-source library for building GUI applications in all flavors of Visual Fortran." For Windows. Open-source.
Proprietary/non-free graphical libraries/GUI
- [DISLIN]: "DISLIN is a high-level plotting library for displaying data as curves, polar plots, bar graphs, pie charts, 3D-color plots, surfaces, contours and maps." For DOS, Linux, Mac OS X, OpenVMS, UNIX, Windows. "Free for non-commercial use" (but not free).
- [SansGUI]: Requires Microsoft Visual C/C++ 6.0+ or Compaq Visual Fortran 6.1+. For Windows.
- [Winteracter]: "The Fortran 9x GUI Toolset." For Linux, Mac OS X, Windows.
Testing Frameworks
- [fUnit] - a unit testing framework.
Miscellaneous
- [comp.lang.fortran] on Usenet
- [Fortran] Open Directory category
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
