Fortran, short for "Formula Translation," is a high-level programming language that is particularly well-suited for scientific and engineering applications. It was one of the first programming languages developed for electronic computers and has evolved considerably since its inception. Fortran allows for efficient manipulation of array data and supports complex mathematical computations, making it a language of choice for numerical weather prediction, computational physics, and bioinformatics, among other fields.
Fortran was developed in the 1950s by IBM as a language designed for numeric computation and scientific computing. The first version, Fortran I, was released in 1957. The core idea was to create a language that would allow scientists and engineers to write programs without needing to know the intricacies of the underlying hardware.
Subsequent versions of Fortran were introduced, each adding new features and improving usability. Fortran II followed shortly, adding structured programming features. In 1966, Fortran IV was introduced, which became an industry standard. Fortran 77 (released in 1978) added features like character data types and improved I/O capabilities.
From 1991 onwards, Fortran 90 introduced array programming, modular programming, recursion, and dynamic memory allocation. Fortran 2003 and Fortran 2008 further enhanced the language with features like Object-Oriented Programming and improved interoperability with the C programming language. The most recent standard, Fortran 2018, brought in further enhancements, including improved compatibility and functionality for parallel processing.
Fortran remains actively used in many scientific and engineering disciplines, supported by modern compilers like GNU Fortran (gfortran) and Intel Fortran Compiler. It has a large legacy codebase, and many scientific libraries are written in Fortran, making it a critical language in certain domains.
Fortran is a strongly typed language that requires explicit declaration of variable types. For example:
INTEGER :: i
REAL :: x
CHARACTER(len=10) :: name
Fortran excels at handling arrays, allowing for operations on entire arrays with simple syntax.
REAL, DIMENSION(10) :: A
A = 2.0 * A ! Multiplies each element of array A by 2
Fortran provides conventional control structures like loops and conditional statements.
DO i = 1, 10
IF (A(i) > 0) THEN
PRINT *, 'Positive'
END IF
END DO
Fortran allows the definition of functions and subroutines for modular programming.
FUNCTION square(x)
REAL :: square
REAL, INTENT(IN) :: x
square = x * x
END FUNCTION square
By default, Fortran has a rule where variables with names beginning with "I", "J", "K", "L", "M", or "N" are implicitly INTEGER. This can be overridden for clarity.
IMPLICIT NONE ! Disables implicit typing
INTEGER :: I
REAL :: R
Fortran supports do-while loops for repeated execution based on a condition.
i = 1
DO WHILE (i <= 10)
PRINT *, i
i = i + 1
END DO
Fortran has powerful user-defined types known as derived types, which allows structure-like data definitions.
TYPE :: Person
CHARACTER(len=20) :: name
INTEGER :: age
END TYPE Person
TYPE(Person) :: p1
Fortran supports modules for encapsulating data and procedures, promoting code reuse and organization.
MODULE myModule
CONTAINS
SUBROUTINE mySubroutine()
! Implementation
END SUBROUTINE
END MODULE
Fortran supports pointers which facilitate dynamic memory management, similar to languages like C.
REAL, POINTER :: pA
ALLOCATE(pA(10)) ! Dynamically allocate an array
Fortran has features creating interoperability with C, allowing for mixed-language programming.
INTERFACE
FUNCTION c_function(x) BIND(C, NAME="c_function")
INTEGER(C_INT) :: c_function(INTEGER)
END FUNCTION c_function
END INTERFACE
The most popular Fortran compilers include:
While Fortran is not as commonly associated with modern IDEs as some other languages, several IDEs support it:
Building a Fortran project typically involves creating source files with a .f, .f90, or .f95 extension and compiling them using a Fortran compiler. A typical command might look like:
gfortran -o my_program my_source.f90
Fortran is extensively used in scientific computing, numerical weather prediction, climate modeling, computational physics, computational chemistry, and bioinformatics. It is particularly known for its performance in heavy numerical computations and simulations.
Fortran's main competitors in the realm of scientific and engineering programming include:
Fortran has several source-to-source translation tools that can help developers convert legacy code to more contemporary languages or to optimize existing code. Some tools include:
These tools ease the transition from older Fortran versions or entirely different programming paradigms while helping maintain performance and functionality.