Fortran: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Paul Wormer
No edit summary
imported>Paul Wormer
No edit summary
Line 1: Line 1:
{{subpages}}
{{subpages}}
'''Fortran''' ('''For'''mula '''tran'''slation) is the oldest high-level [[computer language]].  It was the first  computer language that gave priority to humans over computers, meaning that the early  Fortran programmers did not have to know machine instructions, but  instead could use statements that largely coincide with mathematical formulas.  
'''Fortran''' ('''For'''mula '''tran'''slation) is the oldest high-level [[computer language]].  It was the first  computer language that gave priority to humans over computers, meaning that Fortran users did not have to know machine instructions, as was required of computer programmers before the development of high-level languages. As for any high-level language, Fortran statements are simple transcriptions of mathematical formulas that are independent of the instruction set of the computer on which the program is to be executed.


Fortran being the oldest high-level computer language, there is an enormous legacy in the scientific and engineering communities of Fortran programs, some of them as large as a million statements. It has always been the primary language for intensive (super)computing tasks, such as weather and [[climate modeling]], [[oil reservoir simulation]], computational [[fluid dynamics]], [[computational chemistry]], [[computational economics]], and [[computational physics]]. The existing body of Fortran programs being too large to rewrite into an other computer language in a finite amount of time, it is likely that Fortran is here to stay.  This the more likely, because a very noticeable feature of all  Fortran versions that have appeared  to date is their "upward compatibility". In principle,  a modern Fortran-2003 [[compiler (computer)|compiler]] can still translate a Fortran program written in, say, 1959 and prepare it to run on a  present day computer.
Fortran being the oldest high-level computer language, there is a huge legacy of Fortran programs  in the scientific and engineering communities, with some of the programs exceeding a million statements. Fortran has always been the primary language for intensive (super)computing tasks, such as weather and [[climate modeling]], [[oil reservoir simulation]], computational [[fluid dynamics]], [[computational chemistry]], [[computational economics]], and [[computational physics]]. The existing body of Fortran programs being too large to rewrite into an other computer language in a finite amount of time, it is likely that Fortran is here to stay.  This the more likely, as a very noticeable feature of all  Fortran versions that have appeared  to date is their "upward compatibility". In principle,  a modern Fortran 95 [[compiler (computer)|compiler]] can still translate a Fortran program written in, say, 1959 and prepare it to run on a  present day computer.


The first version of the language was developed in 1954 by an [[IBM]] team lead by [[John Backus]]. This version, known as Fortran I, was mainly restricted to IBM computers (notably the [[IBM 704]] that starting 1957  was delivered with a Fortran compiler). In 1958 a variant, known as Fortran II, was introduced that was soon also implemented by other computer vendors; it now became possible to transfer (Fortran) programs between computers with completely different instruction sets. The issue of [[portability (computers)|portability]] of programs  (possibility of transferral between computers of different architecture) was born.  In 1966 the first standard endorsed by the American Standards Association (now ANSI) was established: Fortran-66. The idea of this standard being that a program that strictly adhered to it would be portable.  
It is not likely that the modern versions of the language  are applied as widely as the earlier versions. Until 1991, when the  Fortran 90 standard was established,  the language lagged behind in modern developments, such as recursion, dynamic memory allocation, object-oriented programming, etc. Many programmers in need of such constructs switched to  [[C]], [[C++]], and variants of these languages, and it is not to be expected that they will  return to the modern forms of Fortran. In any case, by all sorts of metrics Fortran is far behind  C, C++, and Java in popularity among programmers.<ref>[http://www.langpop.com Programming language popularity] Retrieved July 30, 2009</ref>  In the specialized field of [[high performance computing]], however, where computing runs lasting days, or even weeks, are common, Fortran still is the major language. This is because traditionally the Fortran compilers have been&mdash;and still are&mdash;strong in the optimization of computationally intensive tasks.
==History==
The first version of the language was developed in 1954 by an [[IBM]] team lead by [[John Backus]]. This version, known as Fortran I, was mainly restricted to IBM computers (notably the [[IBM 704]] that, starting in 1957, was delivered with a Fortran compiler). In 1958 a variant, known as Fortran II, was introduced that was soon also implemented by other computer vendors; it now became possible to transfer (Fortran) programs between computers with completely different instruction sets. The issue of [[portability (computers)|portability]] of programs  (possibility of transferral between computers of different architecture) was born.  In 1966 the first standard endorsed by the American Standards Association (now ANSI) was established: Fortran 66. The idea of this standard being that a program that strictly adhered to it would be portable. Fortran 66 was largely based on a version, called Fortran IV, developed by IBM around 1961.


From the end of the 1950s onward, many important computer language concepts were developed by the early [[computer science|computer scientists]], such as [[recursion (computer)|recursion]], dynamic storage allocation, local and global variables,  if then else statements, etc. When at the end of the 1960s it was decided that a more modern form of Fortran was needed (that later came to be known as Fortran-77), there was much pressure from the side of (numerical) mathematicians and computer scientists to extend the language with such constructs. However, this was successfully  resisted by programmers&mdash;mainly physical scientists and engineers&mdash;who feared that  the new, more abstract concepts would erode  Fortran's pragmatic, natural style. In addition, workers applying Fortran to numerically intensive tasks were afraid  that the introduction of the constructs would be at the expense of the numerical efficiency of the language. At that time already, very sophisticated  Fortran optimizing compilers were used and it was feared that concepts as recursion would impede the optimization and slow down the programs considerably.  In consequence, the Fortran-77 standard does not allow recursion, dynamic storage allocation, or local variables. It does, however, allow more extended "if statements" and "loop" structures than its predecessors. Also character handling facilities were introduced into Fortran-77.  
From the end of the 1950s onward, many important computer language concepts were developed by the early [[computer science|computer scientists]], such as [[recursion (computer)|recursion]], dynamic storage allocation, local and global variables,  if then else statements, etc. When at the end of the 1960s it was decided that a more modern form of Fortran was needed (that later came to be known as Fortran 77), there was much pressure from the side of (numerical) mathematicians and computer scientists to extend the language with such constructs. However, this was successfully  resisted by programmers&mdash;mainly physical scientists and engineers&mdash;who feared that  the new, more abstract concepts would erode  Fortran's pragmatic, natural style. In addition, workers applying Fortran to numerically intensive tasks were afraid  that the introduction of the constructs would be at the expense of the numerical efficiency of the language. At that time already, very sophisticated  Fortran optimizing compilers were used and it was feared that concepts as recursion would impede the optimization and slow down the programs considerably.  In consequence, the Fortran 77 standard does not allow recursion, dynamic storage allocation, or local variables. It does, however, allow more extended "if statements" and "loop" structures than its predecessors. Also character handling facilities were introduced into Fortran 77.


Fortran-90 that appeared during the early 1990s, allows recursion, dynamic storage allocation, case statements, and a construct that is reminiscent of [[object (computers)|object]]s used in object oriented languages, namely modules. Fortran-90  allows handling of arrays as one entity, which enables compilers to apply parallel processing on them.  
The next Fortran standard  was for quite some known as Fortran 88 because it was expected 11 years after Fortran 77. However, the international standards committees dragged their feet<ref>See [http://www.fortran.com/forsaga.html The Fortran saga] by Brian Meek for a detailed story of the political fights around the establishment of the new standard</ref>  and the first compilers following the new and hotly debated standard  (adopted 11 April, 1991)<ref>Foreword by Michael Metcalf in: W. H. Press, S. A. Teukolsky, W. T. Vetterling, B. P. Flannery: ''Numerical Recipes in Fortran 90''. Cambridge University Press, 1999, ISBN 0-521-57439-0. [http://books.google.nl/books?id=SPEi4mCfhacC&pg=PR10&lpg=PR10&dq=Foreword+by+Michael+Metcalf&source=bl&ots=LPebkCQJci&sig=oBTPY9Wm2FSEik2HGJsYQPaFJlk&hl=nl&ei=O_FzStztBMTl-QaiyrHMBw&sa=X&oi=book_result&ct=result&resnum=1#v=onepage&q=&f=false Online]</ref> started appearing soon after  1991.
Fortran 90 allows recursion, dynamic storage allocation, case statements, and a construct that is reminiscent of [[object (computers)|object]]s used in object oriented languages, namely modules. Very importantly, Fortran 90  allows handling of arrays as one entity, thus enabling compilers to apply parallel processing to them.


The latest standard, Fortran-2003, supports object-oriented and generic programming.
The latest standard, Fortran 2003, supports object-oriented and generic programming.
 
It is not likely that Fortran-90 and Fortran-2003 are applied as widely as the earlier versions of the language. Many programmers in need of modern constructs as recursions, objects and their inheritance, etc., switched during the 1980s  to  [[C]], [[C++]], and variants of these languages. It remains to be seen if they will ever return to the modern forms of Fortran. In any case, by all sorts of measures<ref>[http://www.langpop.com Programming language popularity] Retrieved July 30, 2009</ref> Fortran lags far behind  C, C++, and Java in popularity among programmers. However, in the specialized field of [[high performance computing]], where computing runs lasting days, or even weeks, are common, Fortran still is the major language. This is because traditionally the Fortran compilers have been&mdash;and still are&mdash;strong in the optimization of computationally intensive tasks.


==Some features of the language prior to Fortran 90==
==Some features of the language prior to Fortran 90==
As stated earlier, much old (written before the 1990s) Fortran code is still in use. It is therefore of interest to discuss some of the rules and features of the older versions of the language, many of which are still valid, even in the latest standard.  
As stated earlier, much old (written before the 1990s) Fortran code is still in use. It is therefore of interest to discuss some of the rules and features of the older versions of the language, many of which are still valid, even in the latest standard.


The majority of statements are simply assignment statements of the form
The majority of statements are simply assignment statements of the form
Line 24: Line 25:
===Implicit typing===
===Implicit typing===
Fortran 77 introduced character data. Before that time Fortran only recognized real (floating point) numbers, integer numbers, and logicals (booleans). Names of variables have to begin with a letter.
Fortran 77 introduced character data. Before that time Fortran only recognized real (floating point) numbers, integer numbers, and logicals (booleans). Names of variables have to begin with a letter.
Only logicals (and in Fortran-77 characters) have to be declared explicitly. The real and integer variables are implicitly declared by the rule: all variables with names beginning with I, J, K, L, M, and N are integer, the variables of which the names start with the other letters are real.  
Only logicals (and in Fortran 77 characters) have to be declared explicitly. The real and integer variables are implicitly declared by the rule: all variables with names beginning with I, J, K, L, M, and N are integer, the variables of which the names start with the other letters are real.


The oldest Fortran variants only allowed variable names of maximally 6 characters, which gave rise to names as "CNVRG" for "convergence", "ITHR" for an integer threshold, etc. Moreover, formally the Fortran standard up to and including Fortran 77  prescribed capital letters.
The oldest Fortran variants only allowed variable names of maximally 6 characters, which gave rise to names as "CNVRG" for "convergence", "ITHR" for an integer threshold, etc. Moreover, formally the Fortran standard up to and including Fortran 77  prescribed capital letters.
===Fixed format===
===Fixed format===
Fortran-90 introduced free format, until that time Fortran statements had a fixed format adapted to [[punch card]]s of 80 columns. The first column can contain the letter "C" indicating a comment card. Column 2 to 5 of the card are either blank or contain a numeric label. The 6th column is preserved for a continuation  character; if any non-blank character is in the 6th column, the statement is interpreted as the continuation of the previous statement. Column 7 through 72 contains the actual statement where blanks are completely ignored, they are optionally used to improve readability by humans. Statements are not  closed by a semicolon or otherwise.
Fortran 90 introduced free format, until that time Fortran statements had a fixed format adapted to [[punch card]]s of 80 columns. The first column can contain the letter "C" indicating a comment card. Column 2 to 5 of the card are either blank or contain a numeric label. The 6th column is preserved for a continuation  character; if any non-blank character is in the 6th column, the statement is interpreted as the continuation of the previous statement. Column 7 through 72 contains the actual statement where blanks are completely ignored, they are optionally used to improve readability by humans. Statements are not  closed by a semicolon or otherwise.


===Change of execution flow===
===Change of execution flow===
Line 41: Line 42:
       IF (N .GE. M) GO TO 1234
       IF (N .GE. M) GO TO 1234
</pre>
</pre>
If N &ge; M execution continues with the statement carrying the label 1234.  
If N &ge; M execution continues with the statement carrying the label 1234.


Fortran 77 knows the "IF ('''a''') THEN ... ELSE ... END IF" construct, where '''a''' is either true or false. This construct does not need labels.  
Fortran 77 knows the "IF ('''a''') THEN ... ELSE ... END IF" construct, where '''a''' is either true or false. This construct does not need labels.
===Subroutines and functions===
===Subroutines and functions===
A Fortran program can be partitioned into smaller independent subprograms that at load/link time are combined to one executable program. The most common subprograms are  "subroutines"  that have a number of parameters that are input, output, or both. Also  "functions", having input parameters and returning a single value, are possible. Functions are most commonly used for built-in subprograms provided by the vendor, such as sqrt (square root), etc.
A Fortran program can be partitioned into smaller independent subprograms that at load/link time are combined to one executable program. The most common subprograms are  "subroutines"  that have a number of parameters that are input, output, or both. Also  "functions", having input parameters and returning a single value, are possible. Functions are most commonly used for built-in subprograms provided by the vendor, such as sqrt (square root), etc.


===Common blocks and blank commons===
===Common blocks and blank commons===
As stated, the older Fortran variants did not explicitly distinguish local and global variables. As a matter of fact, most compilers made local copies of variables, and kept arrays global.  
As stated, the older Fortran variants did not explicitly distinguish local and global variables. As a matter of fact, most compilers made local copies of variables, and kept arrays global.


To provide for global variables Fortran has "common blocks", either named or unnamed (blank).  These are memory areas that are assigned to the executable program at load/link time. Each subroutine, in which the common block is declared,  has read/write access to it. Because the common block is an independent programming unit, no information about it other than its starting address, is passed to  subroutine accessing it. In other words, every  subroutine makes its own assumptions about the length and the layout of the common block. It is needless to say that  common blocks in the hands of careless programmers are  almost infinite sources of confusion and programming errors.
To provide for global variables Fortran has "common blocks", either named or unnamed (blank).  These are memory areas that are assigned to the executable program at load/link time. Each subroutine, in which the common block is declared,  has read/write access to it. Because the common block is an independent programming unit, no information about it other than its starting address, is passed to  subroutine accessing it. In other words, every  subroutine makes its own assumptions about the length and the layout of the common block. It is needless to say that  common blocks in the hands of careless programmers are  almost infinite sources of confusion and programming errors.
Line 60: Line 61:
</pre>
</pre>


The statements after the "do statement" up to and including the statement carrying the label 10 are executed, for I varying with unit steps from M to N.  Prior to Fortran 77, the loop was executed at least once, even for M = N+1, i.e., the checking on the range of I was performed at the end of the loop cycle, not at the beginning.  In the majority of cases this is not what the logic of the program dictates, so the older Fortran programs are full of constructs as  
The statements after the "do statement" up to and including the statement carrying the label 10 are executed, for I varying with unit steps from M to N.  Prior to Fortran 77, the loop was executed at least once, even for M = N+1, i.e., the checking on the range of I was performed at the end of the loop cycle, not at the beginning.  In the majority of cases this is not what the logic of the program dictates, so the older Fortran programs are full of constructs as
<pre>
<pre>
       IF (M .GT. N) GO TO 20
       IF (M .GT. N) GO TO 20
Line 70: Line 71:
This was changed in Fortran 77, where by default the loop was skipped when M > N. This change was contrary to the "upward compatibility" philosophy of Fortran and therefore most compilers introduced a "single trip" flag to ask for at least a single trip through the loop, in accordance  with the older standard. Forgetting to set the flag (or not knowing that it is necessary) can cause crashed runs, or even worse, (slightly) wrong results.
This was changed in Fortran 77, where by default the loop was skipped when M > N. This change was contrary to the "upward compatibility" philosophy of Fortran and therefore most compilers introduced a "single trip" flag to ask for at least a single trip through the loop, in accordance  with the older standard. Forgetting to set the flag (or not knowing that it is necessary) can cause crashed runs, or even worse, (slightly) wrong results.


Furthermore, Fortran-77 introduced the following label-free syntax for the do loop:  
Furthermore, Fortran 77 introduced the following label-free syntax for the do loop:
<pre>
<pre>
       DO  I = M, N
       DO  I = M, N
Line 76: Line 77:
       END DO
       END DO
</pre>
</pre>
While also using the "if then else end if" construct, one can write programs in Fortran-77 without any labels or "GO TO" statements. The possibility of  avoiding these was a great step forward.  Prior to Fortran 77, programs were plagued by a proliferation of labels and GO To statements, making older Fortran programs very hard to read by humans.
While also using the "if then else end if" construct, one can write programs in Fortran 77 without any labels or "GO TO" statements. The possibility of  avoiding these was a great step forward.  Prior to Fortran 77, programs were plagued by a proliferation of labels and GO To statements, making older Fortran programs very hard to read by humans.


===Arrays===
===Arrays===
Line 83: Line 84:
       DIMENSION A(100,100), B(25)
       DIMENSION A(100,100), B(25)
</pre>
</pre>
meaning that A(I,J) gives access to an element of the square array A, provided 1 &le; I &le; 100 ''and'' 1 &le; J &le; 100 and B(K), 1 &le; K &le; 25,  gives access to an element of the linear array B. Before Fortran-77 array indices had to be larger than 0. This restriction could be very cumbersome, because the index 0 appears naturally in many problems and the programmer had to shift the index by one, leading to confusion about the choice between &plusmn;1 shifts.
meaning that A(I,J) gives access to an element of the square array A, provided 1 &le; I &le; 100 ''and'' 1 &le; J &le; 100 and B(K), 1 &le; K &le; 25,  gives access to an element of the linear array B. Before Fortran 77 array indices had to be larger than 0. This restriction could be very cumbersome, because the index 0 appears naturally in many problems and the programmer had to shift the index by one, leading to confusion about the choice between &plusmn;1 shifts.
In Fortran 77 upper and lower bounds of arrays are unrestricted.
In Fortran 77 upper and lower bounds of arrays are unrestricted.


==Some features of Fortran-90==
==Some features of Fortran 90==
The next Fortran standard  was for quite some known as Fortran-88 because it was expected 11 years after Fortran-77. However, the international standards committees dragged their feet<ref>See [http://www.fortran.com/forsaga.html The Fortran saga] by Brian Meek for a detailed story of the political fights around the establishment of the new standard</ref>  and the first compilers following the new and hotly debated standard  (adopted 11 April, 1991)<ref>Foreword by Michael Metcalf in: W. H. Press, S. A. Teukolsky, W. T. Vetterling, B. P. Flannery: ''Numerical Recipes in Fortran 90''. Cambridge University Press, 1999, ISBN 0-521-57439-0. [http://books.google.nl/books?id=SPEi4mCfhacC&pg=PR10&lpg=PR10&dq=Foreword+by+Michael+Metcalf&source=bl&ots=LPebkCQJci&sig=oBTPY9Wm2FSEik2HGJsYQPaFJlk&hl=nl&ei=O_FzStztBMTl-QaiyrHMBw&sa=X&oi=book_result&ct=result&resnum=1#v=onepage&q=&f=false Online]</ref> started appearing soon after  1991.
 
In the meantime  Fortran lost much ground in the wake of the downfall of IBM and the rise of [[UNIX]]  workstations and the systems programming language [[C]]. 


The main features of Fortran-90 are
The main features of Fortran 90 are
*  Free format source code form (column independent)
*  Free format source code form (column independent)
*  Modern control structures such as CASE and DO WHILE
*  Modern control structures such as CASE and DO WHILE
Line 97: Line 95:
*  Dynamic memory allocation
*  Dynamic memory allocation
*  Operator overloading
*  Operator overloading
*  Keyword argument passing to procedures
*  Keyword argument passing to procedures
*  The INTENT (in, out, inout) procedure argument attribute
*  The INTENT (in, out, inout) procedure argument attribute
*  Control of numeric precision and range  
*  Control of numeric precision and range
*  Modules - packages containing variable and code
*  Modules - packages containing variable and code


==References==
==References==
<references />
<references />
==External link==
==External links==
[http://www.fortran.com/ A very complete  Fortran site]


* [http://www.fortran.com/ A very complete  Fortran site]
* [http://www.fortran.com/fortran/F77_std/rjcnf0001.html The official Fortran 77  standard]
'''(To be continued)'''
'''(To be continued)'''

Revision as of 04:02, 2 August 2009

This article is developing and not approved.
Main Article
Discussion
Related Articles  [?]
Bibliography  [?]
External Links  [?]
Citable Version  [?]
Catalogs [?]
 
This editable Main Article is under development and subject to a disclaimer.

Fortran (Formula translation) is the oldest high-level computer language. It was the first computer language that gave priority to humans over computers, meaning that Fortran users did not have to know machine instructions, as was required of computer programmers before the development of high-level languages. As for any high-level language, Fortran statements are simple transcriptions of mathematical formulas that are independent of the instruction set of the computer on which the program is to be executed.

Fortran being the oldest high-level computer language, there is a huge legacy of Fortran programs in the scientific and engineering communities, with some of the programs exceeding a million statements. Fortran has always been the primary language for intensive (super)computing tasks, such as weather and climate modeling, oil reservoir simulation, computational fluid dynamics, computational chemistry, computational economics, and computational physics. The existing body of Fortran programs being too large to rewrite into an other computer language in a finite amount of time, it is likely that Fortran is here to stay. This the more likely, as a very noticeable feature of all Fortran versions that have appeared to date is their "upward compatibility". In principle, a modern Fortran 95 compiler can still translate a Fortran program written in, say, 1959 and prepare it to run on a present day computer.

It is not likely that the modern versions of the language are applied as widely as the earlier versions. Until 1991, when the Fortran 90 standard was established, the language lagged behind in modern developments, such as recursion, dynamic memory allocation, object-oriented programming, etc. Many programmers in need of such constructs switched to C, C++, and variants of these languages, and it is not to be expected that they will return to the modern forms of Fortran. In any case, by all sorts of metrics Fortran is far behind C, C++, and Java in popularity among programmers.[1] In the specialized field of high performance computing, however, where computing runs lasting days, or even weeks, are common, Fortran still is the major language. This is because traditionally the Fortran compilers have been—and still are—strong in the optimization of computationally intensive tasks.

History

The first version of the language was developed in 1954 by an IBM team lead by John Backus. This version, known as Fortran I, was mainly restricted to IBM computers (notably the IBM 704 that, starting in 1957, was delivered with a Fortran compiler). In 1958 a variant, known as Fortran II, was introduced that was soon also implemented by other computer vendors; it now became possible to transfer (Fortran) programs between computers with completely different instruction sets. The issue of portability of programs (possibility of transferral between computers of different architecture) was born. In 1966 the first standard endorsed by the American Standards Association (now ANSI) was established: Fortran 66. The idea of this standard being that a program that strictly adhered to it would be portable. Fortran 66 was largely based on a version, called Fortran IV, developed by IBM around 1961.

From the end of the 1950s onward, many important computer language concepts were developed by the early computer scientists, such as recursion, dynamic storage allocation, local and global variables, if then else statements, etc. When at the end of the 1960s it was decided that a more modern form of Fortran was needed (that later came to be known as Fortran 77), there was much pressure from the side of (numerical) mathematicians and computer scientists to extend the language with such constructs. However, this was successfully resisted by programmers—mainly physical scientists and engineers—who feared that the new, more abstract concepts would erode Fortran's pragmatic, natural style. In addition, workers applying Fortran to numerically intensive tasks were afraid that the introduction of the constructs would be at the expense of the numerical efficiency of the language. At that time already, very sophisticated Fortran optimizing compilers were used and it was feared that concepts as recursion would impede the optimization and slow down the programs considerably. In consequence, the Fortran 77 standard does not allow recursion, dynamic storage allocation, or local variables. It does, however, allow more extended "if statements" and "loop" structures than its predecessors. Also character handling facilities were introduced into Fortran 77.

The next Fortran standard was for quite some known as Fortran 88 because it was expected 11 years after Fortran 77. However, the international standards committees dragged their feet[2] and the first compilers following the new and hotly debated standard (adopted 11 April, 1991)[3] started appearing soon after 1991. Fortran 90 allows recursion, dynamic storage allocation, case statements, and a construct that is reminiscent of objects used in object oriented languages, namely modules. Very importantly, Fortran 90 allows handling of arrays as one entity, thus enabling compilers to apply parallel processing to them.

The latest standard, Fortran 2003, supports object-oriented and generic programming.

Some features of the language prior to Fortran 90

As stated earlier, much old (written before the 1990s) Fortran code is still in use. It is therefore of interest to discuss some of the rules and features of the older versions of the language, many of which are still valid, even in the latest standard.

The majority of statements are simply assignment statements of the form

      A = expression

where the expression on the right-hand side is first fully evaluated and then assigned to the variable named A. The arithmetic expressions are the same as in any computer language, with the exception of raising to a power, which is indicated by **, as in 10**3. Comparison operators are between periods: .LE., .EQ., .GE., .GT., .NE.

Implicit typing

Fortran 77 introduced character data. Before that time Fortran only recognized real (floating point) numbers, integer numbers, and logicals (booleans). Names of variables have to begin with a letter. Only logicals (and in Fortran 77 characters) have to be declared explicitly. The real and integer variables are implicitly declared by the rule: all variables with names beginning with I, J, K, L, M, and N are integer, the variables of which the names start with the other letters are real.

The oldest Fortran variants only allowed variable names of maximally 6 characters, which gave rise to names as "CNVRG" for "convergence", "ITHR" for an integer threshold, etc. Moreover, formally the Fortran standard up to and including Fortran 77 prescribed capital letters.

Fixed format

Fortran 90 introduced free format, until that time Fortran statements had a fixed format adapted to punch cards of 80 columns. The first column can contain the letter "C" indicating a comment card. Column 2 to 5 of the card are either blank or contain a numeric label. The 6th column is preserved for a continuation character; if any non-blank character is in the 6th column, the statement is interpreted as the continuation of the previous statement. Column 7 through 72 contains the actual statement where blanks are completely ignored, they are optionally used to improve readability by humans. Statements are not closed by a semicolon or otherwise.

Change of execution flow

The oldest Fortran versions knew only the "arithmetic if". This is a statement of the form

      IF (N) 10, 20, 30

where execution continues at the statements labeled 10, 20, or 30 for N <0 , N = 0 and N > 0, respectively. Labels contained in columns 1 through 5 can be any unique set of up to 4 digits.

Later the "logical if" was added that was usually used in conjunction with a "goto" statement, for example

       IF (N .GE. M) GO TO 1234

If N ≥ M execution continues with the statement carrying the label 1234.

Fortran 77 knows the "IF (a) THEN ... ELSE ... END IF" construct, where a is either true or false. This construct does not need labels.

Subroutines and functions

A Fortran program can be partitioned into smaller independent subprograms that at load/link time are combined to one executable program. The most common subprograms are "subroutines" that have a number of parameters that are input, output, or both. Also "functions", having input parameters and returning a single value, are possible. Functions are most commonly used for built-in subprograms provided by the vendor, such as sqrt (square root), etc.

Common blocks and blank commons

As stated, the older Fortran variants did not explicitly distinguish local and global variables. As a matter of fact, most compilers made local copies of variables, and kept arrays global.

To provide for global variables Fortran has "common blocks", either named or unnamed (blank). These are memory areas that are assigned to the executable program at load/link time. Each subroutine, in which the common block is declared, has read/write access to it. Because the common block is an independent programming unit, no information about it other than its starting address, is passed to subroutine accessing it. In other words, every subroutine makes its own assumptions about the length and the layout of the common block. It is needless to say that common blocks in the hands of careless programmers are almost infinite sources of confusion and programming errors.

Loops

The original form of the subscripted loop ("do loop") is

       DO 10 I = M, N
         ....
   10  (any executable statement)

The statements after the "do statement" up to and including the statement carrying the label 10 are executed, for I varying with unit steps from M to N. Prior to Fortran 77, the loop was executed at least once, even for M = N+1, i.e., the checking on the range of I was performed at the end of the loop cycle, not at the beginning. In the majority of cases this is not what the logic of the program dictates, so the older Fortran programs are full of constructs as

       IF (M .GT. N) GO TO 20
       DO 10 I = M, N
         ....
   10  (any executable statement)
   20  (any executable statement)

This was changed in Fortran 77, where by default the loop was skipped when M > N. This change was contrary to the "upward compatibility" philosophy of Fortran and therefore most compilers introduced a "single trip" flag to ask for at least a single trip through the loop, in accordance with the older standard. Forgetting to set the flag (or not knowing that it is necessary) can cause crashed runs, or even worse, (slightly) wrong results.

Furthermore, Fortran 77 introduced the following label-free syntax for the do loop:

       DO  I = M, N
         ....
       END DO

While also using the "if then else end if" construct, one can write programs in Fortran 77 without any labels or "GO TO" statements. The possibility of avoiding these was a great step forward. Prior to Fortran 77, programs were plagued by a proliferation of labels and GO To statements, making older Fortran programs very hard to read by humans.

Arrays

Basically, arrays in the older Fortran versions are static and declared by a statement of the type

       DIMENSION A(100,100), B(25)

meaning that A(I,J) gives access to an element of the square array A, provided 1 ≤ I ≤ 100 and 1 ≤ J ≤ 100 and B(K), 1 ≤ K ≤ 25, gives access to an element of the linear array B. Before Fortran 77 array indices had to be larger than 0. This restriction could be very cumbersome, because the index 0 appears naturally in many problems and the programmer had to shift the index by one, leading to confusion about the choice between ±1 shifts. In Fortran 77 upper and lower bounds of arrays are unrestricted.

Some features of Fortran 90

The main features of Fortran 90 are

  • Free format source code form (column independent)
  • Modern control structures such as CASE and DO WHILE
  • Handling of whole arrays (array sections, array operators, etc.)
  • Dynamic memory allocation
  • Operator overloading
  • Keyword argument passing to procedures
  • The INTENT (in, out, inout) procedure argument attribute
  • Control of numeric precision and range
  • Modules - packages containing variable and code

References

  1. Programming language popularity Retrieved July 30, 2009
  2. See The Fortran saga by Brian Meek for a detailed story of the political fights around the establishment of the new standard
  3. Foreword by Michael Metcalf in: W. H. Press, S. A. Teukolsky, W. T. Vetterling, B. P. Flannery: Numerical Recipes in Fortran 90. Cambridge University Press, 1999, ISBN 0-521-57439-0. Online

External links

(To be continued)