Fortran: Southern Luzon Technological College Foundation Pio Duran Inc
Fortran: Southern Luzon Technological College Foundation Pio Duran Inc
FORTRAN
The Fortran programming language was one of the first (if not the first) “high level” languages developed for
computers. It is referred to as a high level language to contrast it with machine language or assembly language
which communicate directly with the computer’s processor with very primitive instructions. Since all that a
computer can really understand are these primitive machine language instructions, a Fortran program must be
translated into machine language by a special program called a Fortran compiler before it can be executed.
Since the processors in various computers are not all the same, their machine languages are not all the same.
For a variety of reasons, not all Fortran compilers are the same. For example, more recent Fortran compilers
allow operations not allowed by earlier versions. In this chapter, we will only describe features that one can
expect to have available with whatever compiler one may have available.
Fortran was initially developed almost exclusively for performing numeric computations (Fortran is an
acronym for “Formula Translation”), and a host of other languages (Pascal, Ada, Cobol, C, etc.) have been
developed that are more suited to nonnumeric operations such as searching databases for information. Fortran
has managed to adapt itself to the changing nature of computing and has survived, despite repeated predictions
of its death. It is still the major language of science and is heavily used in statistical computing.
The most standard version of Fortran is referred to as Fortran 77 since it is based on a standard established in
1977. A new standard was developed in 1990 that incorporates some of the useful ideas from other languages
but we will restrict ourselves to Fortran 77.
In late 1953, John W. Backus submitted a proposal to his superiors at IBM to develop a more practical
alternative to assembly language for programming their IBM 704 mainframe computer. Backus’ historic
FORTRAN team consisted of programmers Richard Goldberg, Sheldon F. Best, Harlan Herrick, Peter
Sheridan, Roy Nutt, Robert Nelson, Irving Ziller, Harold Stern, Lois Haibt, and David Sayre. Its concepts
included easier entry of equations into a computer, an idea developed by J. Halcombe Laning and demonstrated
in the Laning and Zierler system of 1952. Some of these programmers were chess players and were chosen to
work at IBM with the thought being that they had logical minds.
A draft specification for The IBM Mathematical Formula Translating System was completed by November
1954. The first manual for FORTRAN appeared in October 1956, with the first FORTRAN compiler delivered
in April 1957. This was the first optimizing compiler, because customers were reluctant to use a high-level
programming language unless its compiler could generate code with performance approaching that of hand-
coded assembly language.
While the community was skeptical that this new method could possibly outperform hand-coding, it reduced
the number of programming statements necessary to operate a machine by a factor of 20, and quickly gained
acceptance. John Backus said during a 1979 interview with Think, the IBM employee magazine, “Much of my
work has come from being lazy. I didn’t like writing programs, and so, when I was working on the IBM 701,
writing programs for computing missile trajectories, I started work on a programming system to make it easier
to write programs.”
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 7090 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.
The development of Fortran paralleled the early evolution of compiler technology, and many advances in the
theory and design of compilers were specifically motivated by the need to generate efficient code for Fortran
programs.
https://www.google.com/url?
sa=t&source=web&rct=j&url=https://www.stat.tamu.edu/~jnewton/604/chap7.pdf&ved=2ahUKEwiDmffmn8L
sAhUvyosBHausCRcQFjAAegQIARAB&usg=AOvVaw2MlEEcXd_ZLOot9MriNYSd
Fortran 2003
Fortran 2003, officially published as ISO/IEC 1539-1:2004, is a major revision introducing many new features.
A comprehensive summary of the new features of Fortran 2003 is available at the Fortran Working Group
(ISO/IEC JTC1/SC22/WG5) official Web site.
From that article, the major enhancements for this revision include:
Derived type enhancements: parameterized derived types, improved control of accessibility, improved
structure constructors, and finalizers
Object-oriented programming support: type extension and inheritance, polymorphism, dynamic type
allocation, and type-bound procedures, providing complete support for abstract data types
Data manipulation enhancements: allocatable components (incorporating TR 15581), deferred type
parameters, VOLATILE attribute, explicit type specification in array constructors and allocate
statements, pointer enhancements, extended initialization expressions, and enhanced intrinsic
procedures
Input/output enhancements: asynchronous transfer, stream access, user specified transfer operations for
derived types, user specified control of rounding during format conversions, named constants for
preconnected units, the FLUSH statement, regularization of keywords, and access to error messages
Procedure pointers
Support for IEEE floating-point arithmetic and floating point exception handling (incorporating TR
15580)
Interoperability with the C programming language
Support for international usage: access to ISO 10646 4-byte characters and choice of decimal or comma
in numeric formatted input/output
Enhanced integration with the host operating system: access to command line arguments, environment
variables, and processor error messages
An important supplement to Fortran 2003 was the ISO technical report TR-19767: Enhanced module facilities
in Fortran. This report provided sub-modules, which make Fortran modules more similar to Modula-2 modules.
They are similar to Ada private child sub-units. This allows the specification and implementation of a module
to be expressed in separate program units, which improves packaging of large libraries, allows preservation of
trade secrets while publishing definitive interfaces, and prevents compilation cascades.
Fortran 2008
ISO/IEC 1539-1:2010, informally known as Fortran 2008, was approved in September 2010. As with Fortran
95, this is a minor upgrade, incorporating clarifications and corrections to Fortran 2003, as well as introducing
some new capabilities. The new capabilities include:
Sub-modules—additional structuring facilities for modules; supersedes ISO/IEC TR 19767:2005
Coarray Fortran—a parallel execution model
The DO CONCURRENT construct—for loop iterations with no interdependencies
The CONTIGUOUS attribute—to specify storage layout restrictions
The BLOCK construct—can contain declarations of objects with construct scope
Recursive allocatable components—as an alternative to recursive pointers in derived types
Fortran 2018
The latest revision of the language (Fortran 2018) was earlier referred to as Fortran 2015. It is a significant
revision and was released on November 28, 2018.
KEY CODES
FORTRAN
The initial release of FORTRAN for the IBM 704 contained 32 statements, including:
DIMENSION and EQUIVALENCE statements
Assignment statements
Three-way arithmetic IF statement, which passed control to one of three locations in the program
depending on whether the result of the arithmetic statement was negative, zero, or positive
IF statements for checking exceptions ( ACCUMULATOR OVERFLOW , QUOTIENT
OVERFLOW , and DIVIDE CHECK ); and IF statements for manipulating sense switches and sense
lights
GO TO , computed GO TO , ASSIGN , and assigned GO TO
DO loops
Formatted I/O: FORMAT , READ , READ INPUT TAPE , WRITE , WRITE OUTPUT
TAPE , PRINT , and PUNCH
Unformatted I/O: READ TAPE , READ DRUM , WRITE TAPE , and WRITE DRUM
Other I/O: END FILE , REWIND , and BACKSPACE
PAUSE , STOP , and CONTINUE
FREQUENCY statement (for providing optimization hints to the compiler).
The arithmetic IF statement was reminiscent of (but not readily implementable by) a three-way comparison
instruction (CAS—Compare Accumulator with Storage) available on the 704. The statement provided the only
way to compare numbers—by testing their difference, with an attendant risk of overflow. This deficiency was
later overcome by "logical" facilities introduced in FORTRAN IV.
The FREQUENCY statement was used originally (and optionally) to give branch probabilities for the three
branch cases of the arithmetic IF statement. The first FORTRAN compiler used this weighting to perform at
compile time a Monte Carlo simulation of the generated code, the results of which were used to optimize the
placement of basic blocks in memory—a very sophisticated optimization for its time. The Monte Carlo
technique is documented in Backus et al.'s paper on this original implementation, The FORTRAN Automatic
Coding System: Many years later, the FREQUENCY statement had no effect on the code, and was treated as a
comment statement, since the compilers no longer did this kind of compile-time simulation. A similar fate has
befallen compiler hints in several other programming languages, e.g. the register keyword in C.
FORTRAN II (1958)
IBM's FORTRAN II appeared in 1958. The main enhancement was to support procedural programming by
allowing user-written subroutines and functions which returned values with parameters passed by reference.
The COMMON statement provided a way for subroutines to access common (or global) variables. Six new
statements were introduced:
Over the next few years, FORTRAN II would also add support for the DOUBLE
PRECISION and COMPLEX data types.
Early FORTRAN compilers supported no recursion in subroutines. Early computer architectures supported no
concept of a stack, and when they did directly support subroutine calls, the return location was often stored in
one fixed location adjacent to the subroutine code (e.g. the IBM 1130) or a specific machine register (IBM
360 et seq), which only allows recursion if a stack is maintained by software and the return address is stored on
the stack before the call is made and restored after the call returns. Although not specified in FORTRAN 77,
many F77 compilers supported recursion as an option, and the Burroughs mainframes, designed with recursion
built-in, did so by default. It became a standard in Fortran 90 via the new keyword RECURSIVE.
IBM also developed a FORTRAN III in 1958 that allowed for inline assembly 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
IBM began development of FORTRAN IV starting in 1961, as a result of customer demands. FORTRAN
IV removed the machine-dependent features of FORTRAN II (such as READ 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, IBM 7094, and later for the IBM 1401 in
1966.
By 1965, FORTRAN IV was supposed to be compliant with the standard being developed by the American
Standards Association X3.4.3 FORTRAN Working Group.
Between 1966 and 1968, IBM offered several FORTRAN IV compilers for its System/360, each named by
letters that indicated the minimum amount of memory the complier needed to run. The letters (F, G, H)
matched the codes used with System/360 model numbers to indicate memory size, each letter increment being a
factor of two larger:
1966 : FORTRAN IV F for DOS/360 (64K bytes)
1966 : FORTRAN IV G for OS/360 (128K bytes)
1968 : FORTRAN IV H for OS/360 (256K bytes)
At about this time FORTRAN IV had started to become an important educational tool and implementations
such as the University of Waterloo's WATFOR and WATFIV were created to simplify the complex compile
and link processes of earlier compilers.
FORTRAN 66
Perhaps the most significant development in the early history of FORTRAN was the decision by the American
Standards Association (now American National Standards Institute (ANSI)) to form a committee sponsored by
BEMA, the Business Equipment Manufacturers Association, to develop an American Standard Fortran. The
resulting two standards, approved 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 the first standard, officially denoted X3.9-1966,
became known as FORTRAN 66 (although many continued to term it FORTRAN IV, the language on which the
standard was largely based). FORTRAN 66 effectively became the first industry-standard version of
FORTRAN. FORTRAN 66 included:
END line
FORTRAN 77
After the release of the FORTRAN 66 standard, compiler vendors introduced several extensions to Standard
Fortran, prompting ANSI committee X3J3 in 1969 to begin work on revising the 1966 standard, under
sponsorship of CBEMA, the Computer Business Equipment Manufacturers Association (formerly BEMA).
Final drafts of this revised standard circulated in 1977, leading to formal approval of the new FORTRAN
standard in April 1978. The new standard, called FORTRAN 77 and officially denoted X3.9-1978, added a
number of significant features to address many of the shortcomings of FORTRAN 66:
Block IF and END IF statements, with optional ELSE and ELSE IF clauses, to provide improved
language support for structured programming
DO loop extensions, including parameter expressions, negative increments, and zero trip counts
OPEN , CLOSE , and INQUIRE statements for improved I/O capability
Direct-access file I/O
IMPLICIT statement, to override implicit conventions that undeclared variables are INTEGER if their
name begins with I, J, K, L, M, or N (and REAL otherwise)
CHARACTER data type, replacing Hollerith strings with vastly expanded facilities for character input
and output and processing of character-based data
PARAMETER statement for specifying constants
SAVE statement for persistent local variables
Generic names for intrinsic functions (e.g. SQRT also accepts arguments of other types, such
as COMPLEX or REAL*16 ).
A set of intrinsics ( LGE, LGT, LLE, LLT ) for lexical comparison of strings, based upon
the ASCII collating sequence. (These ASCII functions were demanded by the U.S. Department of
Defense, in their conditional approval vote).
In this revision of the standard, a number of features were removed or altered in a manner that might invalidate
formerly standard-conforming programs. (Removal was the only allowable alternative to X3J3 at that time,
since the concept of "deprecation" was not yet available for ANSI standards.) While most of the 24 items in the
conflict list (see Appendix A2 of X3.9-1978) addressed loopholes or pathological cases permitted by the prior
standard but rarely used, a small number of specific capabilities were deliberately removed, such as:
DIMENSION A(10,5)
Y= A(11,1)
Transfer of control out of and back into the range of a DO loop (also known as "Extended Range")
A Fortran program consists of a series of lines of code. These lines are executed in the order that they appear
unless there are statements that cause execution to jump from one place in the series of lines to another. At the
end of this chapter, we have listed the code of a complete program and we will refer to this program as we go
along. Learning to write Fortran programs is usually done by imitating programs written by others. This chapter
cannot possibly provide all of the details of Fortran, but rather to point out those features that are particularly
important in statistical computing.
1. Statements begin in column 7 of a line and end before column 72 (if a statement will not fit in
columns 7–72, it can be continued onto the next line (or lines) as long as a character is placed in
column 6 of each continuation line). Columns 1–5 of a line can contain “a statement number” so
that the statement on that line can be referenced by other statements. Lines that begin with the
letter c in column one are interpreted as comments and are not executed. It is important to
include enough comments in one program in any language so that you (or somebody else) can
later understand what the lines of cone are intended to do.
2. A file containing a Fortran program begins with a main program which may be followed by
subprogram.
3. Subprograms come in two kinds; functions and subroutines. The main program and the
subprogram it calls can be in different files which can be compiled separately and then linked
together to form an executable file.
4. A main program begins with a series of statements declaring the names of the variables that are
going to be used by the program and what type of variables they are (see “data types” below).
Note that variable names must begin with letters of the alphabet, contain letters and the digits 0
through 9, and (to be safe as different compilers allow different numbers of characters) contain 6
characters or fewer. The program ends with the lines stop and end.
5. Between the declarations and the stop and end there are a wide variety of operations that can be
performed, but usually a program consists of 1) reading some information from somewhere (for
example, from a file or from the keyboard from the person using the program 2) performing
some numerical or graphical task, and 3) placing the results of the task somewhere so that the
user of the program can use them (perhaps on the user’s screen or into a file).
6. Often there is some subtask that needs to be performed several times in a program. For example,
it may be necessary to form the plot of one vector versus another. Rather than have the same set
of lines of code in several different places in the main program, one can form them into what is
called a subprogram. A subprogram begins and ends with special lines of code that tell the
compiler that it is in fact a subprogram and not part of the main program. Another use of
subprograms is in modular programming where one breaks a complicated task into a series of
tasks that are more manageable and writing a subprogram for each one.
program average
implicit none
allocate (points(number_of_points))
deallocate (points)
According to Craig Dedo a former member of ISO/ANSI J3 Fortran standards committee: All kinds of
applications are built with Fortran. Fortran is very well suited to developing a wide variety of applications,
even though its strongest area is very heavy duty number crunching.
I use Fortran as my first choice because of its grammatical simplicity and high expressive power, i.e., the
ability to get a lot of software functionality with a minimum of time and effort. I also like it because it
almost always allows me to get the job done straightforwardly without getting caught up in esoteric
gotchas.
I have used Fortran to develop many different kinds of applications, including, but not limited to:
Financial analysis
Engineering design