Difference between revisions 3101915 and 3234170 on enwikibooks

{{split}}
[[File:CCBYSA yellow.png]]

=Preface=
The goal of this Fortran tutorial is to give a quick introduction to the most common features of the Fortran 77 programming language. A companion tutorial introduces the key enhancements found in Fortran 90. It is not a complete reference! Many details have been omitted. The presentation focuses on scientific computations, mainly linear algebra. The outline of this tutorial was inspired by the book "Handbook for Matrix Computations" by T.F. Coleman a(contracted; show full)

==Portability==

A major advantage Fortran has is that it is standardized by ANSI and ISO (see footnotes). Consequently, if your program is written in ANSI Fortran 77, using nothing outside the standard, then it will run on any computer that has a Fortran 77 compiler. Thus, Fortran programs are portable across machine platforms. (If you want to read some Fortran Standards Documents, click [http://www.nag.co.uk/sc22wg5/ here].)

==Footnotes
:==
 '''ANSI''' = American National Standards Institute
 '''ISO''' = International Standards Organization

=Fortran 77 Basics=
A Fortran program is just a sequence of lines of text. The text has to follow a certain structure to be a valid Fortran program. We start by looking at a simple example:
<source lang="fortran">
      program circle
      real r, area
(contracted; show full)      real
      dble
      ichar
      char
</source>
The first three have the obvious meaning. ichar takes a character and converts it to an integer, while char does exactly the opposite.

===Example
:===  
How to multiply two real variables x and y using double precision and store the result in the double precision variable w:
<source lang="fortran">
      w = dble(x)*dble(y)
</source>
Note that this is different from
<source lang="fortran">
      w = dble(x*y)
(contracted; show full)    STATUS=  sta
    ACCESS=  acc
    FORM=    frm
    RECL=    rl</source>
The unit number u is a number in the range 1-99 that denotes this file (the programmer may chose any number but he/she has to make sure it is unique).

*<code>iostat</code> is the I/O status identifier and should be an integer variable. Upon return, ios is zero if the statement was successful and returns a non-zero value otherwise.



*<code>err</code> is a label which the program will jump to if there is an error.


*<code>fname</code> is a character string denoting the file name.


*<code>sta</code> is a character string that has to be either NEW, OLD or SCRATCH. It shows the prior status of the file. A scratch file is a file that is created when opened and deleted when closed (or the program ends).


*<code>acc</code> must be either SEQUENTIAL or DIRECT. The default is SEQUENTIAL.


*<code>frm</code> must be either FORMATTED or UNFORMATTED. The default is UNFORMATTED.


*<code>rl</code> specifies the length of each record in a direct-access file.

For more details on these specifiers, see a good Fortran 77 book.

After a file has been opened, you can access it by read and write statements. When you are done with the file, it should be closed by the statement
<source lang="fortran">
      close ([UNIT=]u[,IOSTAT=ios,ERR=err,STATUS=sta])</source>
where, as usual, the parameters in brackets are optional.
(contracted; show full)

When you have if-then-elseif statements with multiple conditions, try to place the most likely conditions first. This exploits the branch-prediction logic in modern microprocessors. Branch prediction is used to keep the microprocessor pipeline as full as possible by starting to execute instructions following a predicted branch path. If the prediction turns out to be wrong, the pipeline stalls while intermediate results are discarded and instructions from the other path are scheduled.
  

If possible, floating point and integer statements can be mixed together so that the processor
can have a mixture of floating point and integer instructions to keep the different execution units in each pipeline busy. Some of these optimizations can be done by the Fortran compiler but others must be done  by hand. Compilers must consider the possibility that the arrays used for matrix operations may overlap, so many optimizations must be done by the programmer.

=Debugging hints=
(contracted; show full)nter quarter 1996. It has been modified by Sarah T. Whitlock and Paul H. Hargrove for use in the Fortran courses which have been offered under different course numbers each subsequent year. The original source of the material is here http://www.stanford.edu/class/me200c/tutorial_77/ Stanford university has re-released the material under a creative commons 3.0 attribution license. The tutorial was transferred to mediawiki format by Houraa Daher.

{{Subjects|Fortran programming language}}
{{alphabetical|F}}

{{status|100%}}

==References==
{{Reflist}}