Information AboutFortran |
|
The name is a Portmanteau derived from its origins as the ''IBM Mathematical Formula '''Tran'''slation System''. 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." Fortran is Statically Typed , Compiled (though sometimes Interpreted ), and Imperative . Since FORTRAN II (1958), it has been a Procedural Programming language. Recent versions of Fortran have included some features to support Vector (Fortran 90), Object-based (Fortran 95), Object-oriented and Generic (Fortran 2003) programming styles. 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 Compiler s were motivated by the need to generate good code for Fortran programs. Early versions of the language were specified by vendors and could differ from machine to machine. IBM's FORTRAN II appeared in 1958 , and FORTRAN IV in 1961 . FORTRAN II only had a three way ''IF'' ( "IF (X-Y) 10,20,30"), that branched to one of three statement numbers based on whether a numeric expression was negative, zero, or positive; FORTRAN IV added boolean expressions and logical IF tests. (IBM designed a FORTRAN III in 1958 that allowed for inline assembler code, among other features, but it was never released as a product because the portability advantage of a High-level Language would be lost.) The first industry-standardized version was FORTRAN 66 in 1966 , followed by FORTRAN 77 in 1977 , Fortran 90 in 1990 , Fortran 95 in 1995 , and Fortran 2003 in 2003 . Early FORTRAN programs were handwritten on Coding Form s or plain paper and then punched on Punch Cards . Formatting rules were rigid:
As programs moved to magnetic media, vendors offered different extensions to increase line lengths beyond 72 characters. Blanks were, in general, not significant in early FORTRAN programs, so that a statement such as DO 1 I = 1, 10 could equivalently be punched DO1I=1,10 Minor typos could easily lead to wildly different semantics; for example, if the comma above were inadvertently punched as a dot, the above statement (intended to implement a DO loop) would be interpreted as an assignment statement which would create a new REAL variable DO1I and give it the value 1.10:DO1I = 1.10 Symbolic names in early compilers were usually limited to the number of alphanumeric characters that fit in one machine Word ; six characters was typical, but the maximum could be as few as three. Both the FORTRAN 66 and FORTRAN 77 standards required compilers to support up to six characters in a symbolic name, though many compilers were extended to support larger names. The maximum length of a name was raised to 31 characters in Fortran 90, and to 63 in Fortran 2003. By default, numeric variables did not need to be declared; variables whose names began with the letters I through N were treated as INTEGER variables, whereas all other variables were assumed to be REALs. (In mathematics, the symbols ''i'' through ''n'' are typically used to denote subscripts and superscripts, which are integers.) This ''implicit typing'' mechanism could be overridden using an IMPLICIT statement, or by explicitly declaring the type of variables (also necessary for variables with other than REAL or INTEGER type). Furthermore, because simply mistyping a variable name caused the creation of a new variable with the mistyped name, later implementations provided an IMPLICIT NONE statement to turn off the implicit typing mechanism, a feature standardized with Fortran 90.STANDARD VERSIONS FORTRAN-66: Brought together capabilities common to a number of early FORTRAN implementations. There were notable simplifications - especially in the area of I/O (e.g., no READ INPUT TAPE of early implementations). Supported features included:
FORTRAN-77: Added a number of features to address problem areas of FORTRAN-66:
An important enhancement to FORTRAN-77 was the release of MIL-STD-1753 in 1978. The MIL-STD-1753 features were implemented by most FORTRAN-77 compilers, and later incorporated into Fortran 90:
Fortran 90: A major revision, adding:
Fortran 95: A relatively minor revision involving mostly cleanups. However it also adds a few capabilities from the High Performance Fortran dialect:
An important follow-on to Fortran-95 was TR-15581 - the "Allocatable TR". This specification allows enhanced use of ALLOCATABLE arrays, prior to the availability of Fortran 2003. Such uses include ALLOCATABLE arrays as derived type components, in procedure dummy argument lists, and as function return values.Fortran 2003: A major revision adding:
A comprehensive summary of the 2003 additions is available 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. The ENTRY statement even allowed program control to be transferred to any statement ''within'' the subprogram being called, violating the ''single-entry'' control flow philosophy of modern programming practice. These obsolescent features have been deprecated in more recent versions of the language. Successive versions have introduced 'modern' programming constructs such as inline Comments , output of text, the block IF structure (introduced 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 unstructured and deprecated features of Fortran, such as EQUIVALENCE. Vendors of high-performance scientific computers ( Burroughs , CDC , Cray , Honeywell , IBM , Texas Instruments , UNIVAC , etc.) added extensions to Fortran to make use of special hardware features such as instruction Cache , CPU Pipeline , and vector arrays. For example, one of IBM's Fortran compilers (H Extended IUP) had a level of optimization which reordered the Machine Language Instruction s to keep several internal arithmetic units busy at the same time. Another example is CFD, a special variant of Fortran designed specifically for the ILLIAC IV supercomputer, running at NASA 's Ames Research Center . These extensions have either disappeared over time or have 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. 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. If deprecated features are used 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. Fortran can rightly be criticized for issues due to its history as one of the first scientific programming languages: its archaic syntax, arbitrary conventions such as implicit typing and fixed-form source input, and support for bad programming practices. But it is still an efficient language—indeed it sacrifices elegance for efficiency—and there is a huge legacy of useful software written in various versions of Fortran. Other scientific programming languages have been designed with the intention of keeping the advantages of Fortran without the disadvantages (e.g., ALGOL ), but Fortran is still with us and likely to remain so for a long time. 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 supercomputing tasks, such as weather/climate modeling and simulation of automobile crash dynamics. Indeed, one finds that even today, half a century later, floating-point benchmarks to gauge the performance of computer processors are still written in Fortran (e.g., CFP2000 , the floating-point component of the SPEC CPU2000 benchmarks), a tribute to the longevity of the language. SAMPLE PROGRAMS The following programs can be compiled and run with any Fortran 90 or 95 compiler, such as GFortran . Most modern Fortran compilers expect a file with a .f extension (for FORTRAN 77 source) or .f90/.f95 extension (for Fortran 90/95 source).Hello, world! Here is a ''HELLO, WORLD!'' example written in FORTRAN IV: C IT WAS THE FIRST PROGRAMMING LANGUAGE C WITH SUPPORT FOR COMMENTS WRITE (6,7) 7 FORMAT(13HHELLO, WORLD!) STOP END This program prints ''HELLO, WORLD!'' to Fortran unit number 6 (on most machines, 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 can 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 a WRITE statement which invokes it, or they are grouped together at the end of the program or subprogram block). The first few characters 13H in the FORMAT statement above define a ''Hollerith constant'' (or ''Hollerith literal''), meaning that the immediately following 13 characters are to be taken as a character constant; note that no delimiters are used here. (Some compilers also permitted character literals enclosed in single quotes, a practice that came to be standard with FORTRAN 77.)As of FORTRAN 77, single quotes are used to delimit character literals, and inline character strings may be used instead of references to FORMAT statements: PRINT '(A, F6.2)', 'Total: ', X Also 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, would be written in Fortran 90 as follows: program HelloWorld
end program HelloWorld Example: Cylinder area This program calculates the area of a cylinder.
! If radius and height could not ! be read from input, then restart ! the loop. if (ierr /= 0) then
cycle interactive_loop end if
! Write the input variables (radius, height) ! and output (area) to the screen.
'radius=',radius,'height=',height,'area=',area yn = ' ' yn_loop: do
'Y' .OR. YN 'N' .OR. YN 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 statements. Also note the use of descriptive variable names and general code formatting that comport with contemporary computer programming style. The program performs some averaging on interactively entered data.
allocate (Points(NumberOfPoints))
!!! take the average by summing Points and dividing by NumberOfPoints if (NumberOfPoints>0) AveragePoints = sum(Points)/NumberOfPoints !!! now form average over positive and negative points only if (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 terminal
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. In the earliest versions of FORTRAN, the only branching statement available was the ''Arithmetic IF Statement'', a kind of "3-way GOTO " statement; this statement corresponded directly to the IBM 704 branch instruction, which was a three-way branch. The syntax of the arithmetic IF is IF (INTEGER or REAL expression) n1, n2, n3 Control passes to statement number n1 if the expression is negative, to n2 if it is exactly zero, and to n3 if positive. Comparison of REAL numbers requires care with the arithemtic IF and elsewhere. If branching depends upon a REAL number being zero or two REALs being equal, it is very common, particularly with early compilers, that numbers which should be the same but were calculated in different program paths will differ by a small rounding error. For example the comparison
may fail, jumping to statements 100 or 300 rather than the desired 200. An example is: IF (I - J) 100, 200, 300 The expression in parentheses may be integer- or real-valued (in this example, let us assume the expression is integer-valued for simplicity). Control passes directly to statement number 100, 200, or 300, depending on whether the expression I - J is negative, zero, or positive, respectively.Mentally it is convenient to interpret the above arithmetic IF statement as an arithmetic comparison with branch, testing whether I is less than, equal to, or greater than J. Thus, in FORTRAN 66, which introduced the ''Logical IF Statement'', the arithmetic IF statement above could equivalently be expanded to:IF (I .LT. J) GO TO 100 IF (I .EQ. J) GO TO 200 IF (I .GT. J) GO TO 300 For the cases of "less than or equal" or "greater than or equal," the first two or last two, respectively, of the GOTO targets would be the same. FORTRAN 77 introduced the ''Block IF Statement'' with optional ''ELSE IF'' and ''ELSE'' clauses. In addition, Fortran 90 also introduced the case selection structure, which can be used to implement multi-way branches such as the above without the use of the deprecated GOTO statement. For example, the (perhaps now overworked) example above could be recast in Fortran 90 as follows:
ADVANCED SAMPLE PROGRAMS The following examples are intended to be compiled and run with any Fortran compiler that conforms to the Fortran 95 Standard. Procedures Modern Fortran features available for use with Procedures , include deferred-shape and protected and optional arguments, are illustrated in the following example.
Iter = 0 !Compute solution until convergence convergence_loop: do while (TolMax >= Tol.AND.Iter < NumIter); Iter = Iter + 1 TolMax = -1. !Reset the tolerance value !Compute solution for the k-th iteration iteration_loop: do i = 1, n !Compute the current x-value
!Compute the error of the solution
X(i) = Xk enddo iteration_loop enddo convergence_loop if (present(ActualIter)) ActualIter = Iter GaussSparse = TolMax end function GaussSparse If return values are to be passed via the procedure's arguments, a Subroutine is preferred over a function, as follows:
Modules A Module is program unit which contains both data and procedures. The only difference between a module and a regular program in Fortran is that a module contains no executable main PROGRAM unit.
!Variables integer :: n, m, retint
character(50) :: AppName !Arrays real, allocatable, dimension(:,:,:) :: a, b, c, d
!Structures type ijk integer i integer j integer k end type ijk type 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 AppName public private AppName !Procedure overloading interface swap module procedure swap_Integer, swap_Real end interface swap interface GetLastError !This adds one more procedure to the generic procedure GetLastError module procedure GetLastError_GlobalModule end interface GetLastError !Operator overloading interface operator(+) module procedure add_ijk end interface !Prototype for external procedure interface 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 module contains !Internal function function 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 files include 'Swap_Integer.f90' !Comments SHOULDN'T be added here include 'Swap_Real.f90' end module GlobalModule Pointers and Targets In Fortran, the concept of Pointer differs from that in C -like languages in that a pointer does not store the memory address of another variable (unless it is defined as an integer, and a compiler-supplied function is used to do so, or the compiler provides a C-pointer feature). Instead, it serves either as an alias for another variable (or part of it), or as an ordinary, dynamically allocated variable. If the pointer is an alias, it is said that its status is "associated," and the variable to which it points must have either the "pointer" or "target" attribute. The following example illustrates the concept:
STANDARD FORTRAN JOKES Not surprisingly, FORTRAN has its share of jokes:
REFERENCES General: Standards documents:
Other Standards documents:
EXTERNAL LINKS General
Program repositories
Proprietary compilers Free software compilers
Graphical libraries and GUI
Online books
|
|
|