Monday, June 27, 2011

What is FORTRAN? - An Introduction

FORTRAN is acronym FORmula TRANslator. It is a high-level programming language developed to handle formulae, equations, functions, files, and related programming operations of various types. It is mostly used by scientists and engineers to handle complex equations, formulae and variables arising from their experimental and analytic works. However, the flexibility of recent types and versions of FORTRAN has made it to be applied in other fields of human endeavours other than in sciences and engineering fields only.

There abound many types and versions of FORTRAN Programming Language such as FORTRAN IV, FORTRAN 77, FORTRAN 88, FORTRAN 90, WATFOR (or WATLOO FORTRAN), Microsoft FORTRAN, etc. but for the purpose of uniformly and proper understanding, only FORTRAN 77 is used in this website. So, where FORTRAN is used in this site, it means FORTRAN 77.

FORTRAN Character Set
FORTRAN statements, keywords or reserved word are from a set of characters. These characters are the 26 English alphabets (both uppercase lowercase letters A,B,C…,X,Y,Z, a,b,c…,x,y,z; the ten digits 0 to 9; the blank space; and the following special characters:
, * / + - . ( ) : ' =

The uses of the special characters will be explored as we forge ahead. However, any character can be used where string literals or comments are involved.

FORTRAN coding Sheet
It is just very important to state and make it abundantly clear from the onset that FORTRAN statements are coded (written) in a special format different from many other high-level programming languages, but very much similar to COBOL coding, as shown in the following figure.








Column 1 (first column) in a line is used to indicate a comment line. The letter C or an asterisk (*) is used in the column to indicate that the line is a comment line. Columns 1 to 5 are used for statement labels (statement labels are usually positive numbers such as the statement line numbers or FORMAT line numbers. Note that some few versions of FORTRAN restrict the statement label columns to columns 2 to 5 only.). Column 6 is used to indicate the continuation of statement that exceeds the 72nd column. Any character, except blank and zero, can be placed in column 6 to indicate the continuation of a statement begun on the preceding line(s). However, if the statement fits into one line, then column 6 must be left blank. FORTRAN statement itself appears between columns 7 and 72 inclusively. Any character in a FORTRAN statement line beyond column 72 are ignored. Therefore, columns 73 and above may be used by the programmer for a sequential line numbering or other identifications or anything else or they may be left blank.

Except with a literal string, blank columns in a FORTRAN statement have no meaning and therefore should be use freely to improve the appearance of the program. For example, we can use extra blanks on each side of an operator such as + or *, and wee can indent some lines of our program to make the structure of the program to be much obvious. These extra blanks makes the program easier to understand but they are ignored by the FORTRAN compiler.

Placement of FORTRAN Program statement
Though we have not written any program yet, it is wise we know from the beginning which type of statements should precede or follow a given statement in a program.

The order in which statements should be placed in a program is as below

1.PROGRAM: The first statement, except the comment line which can appear anywhere in a program, should be the PROGRAM statement. Likewise, in a function and a subroutine, the first statement must be FUNCTION and SUBROUTINE, respectively.
2. The data type declaration such as IMPLICIT, PARAMETER, REAL, INTEGER CHARACTER. LOGICAL, DOUBLE PRECISION, and DIMENSION statements should come after the PROGRAM statement.
3. The COMMON and EQUIVALENCE statements should follow
4. DATA statements.
5. Statement Function definition
6. FORTRAN Executable Statements, including FORMAT statement
7. END statement.

In general, all declarations and definitions (except the FORMAT declaration) must precede the executable statements.

Note that if the above order is not followed, then “statement out of sequence” error during compilation of the program will occur.

How to Enter and Run FORTRAN Programs

A FORTRAN program can be typed (keyed in) and saved using any ASCII text editor such as Sidekick (SK), WordStar Release 4(WS4), DOS line editor (EDLIN), dBASE III PLUS, dBASE IV, Turbo C and PASCAL integrated development environments, COPY CON command of DOS, or any package that stores texts exactly as they were entered. The program is often saved with file extension .FOR, though any file extension can be used. Note that when typing the program, the coding format treated in section 1.3 must be observed.

After keying in the program and saved, then, compile the program using the FORTRAN compiler to create any object file that will have the extension .OBJ. In most cases, the compiler is normally F77.EXE, or FORTRAN77.EXE, or FORTRAN.EXE, or any name given to the compiler file. As an example, to compile a FORTRAN program that has been saved with the name PROG.FOR, we type, at the DOS directory that contains the compiler file, the following

F77 PROG
or
F77 PROG.FOR

This will request you to press the return key(i.e ENTER key) to continue. When the return key is pressed, the following message will appear:

Fortran 77 Compiler – Version 3.15 17 FEB 96 09:30:45
(C) Copyright FortranSoft Ltd 1987 --------------

******Lines Compiled = x ******
****** Compilation Successful *****

where x is the total number of lines in the program. If the program has some errors, then the return key is press, the following message

***** x errors – No object produced *****

After compiling the above program, the object file PROG.OBJ is automatically created. The next stage is to create the executable file with extension.EXE by the use of any linker (LINK.EXE) program. So, having compiled the program as described above, we create (or link) an executable file by issuing the command
LINK PROG

where PROG is the source program name. The following messages will appear:
Microsoft (R) 8086 Object linker Version 3.05
Copyright (C) Microsoft corp. 1983, 1984, 9185. All right reserved.

Run file.[PROG.EXE]

You can then type a new name for the executable file or you press the ENTER key to use the default name PROG.EXE. Next the listing and library files will be requested for, in turn, thus:

List File [NUL.MAP]
LIBRARIES [.LIB]:

Supply new file names or press the ENTER key for none, in each case. The executable file PROG.EXE is now created and can be executed, independent of the source nd object files, by simply typing its name at the DOS command line, thus:

PROG


NB: The above compilation procedure may differ from version to version of FORTRAN 77 (Consult your FORTRAN 77 Manual or supplier of FORTRAN packages for more information on how to run your v programs.).