FORTRAN is a general-purpose, the first-ever high-level programming language used for mathematical computations. It is an acronym for FORmula TRANslation. It was started in the 1950s at IBM and since then many updated versions of FORTRAN have been released.
- Fortran 66
- Fortran 77
- Fortran 90
- Fortran 2003
- Fortran 2008
FORTRAN 77 is still very popular among the users despite the great development in the field. This language is standardized by ANSI and ISO and hence has the advantage of easy portability across the machine platforms.
Why Learn FORTRAN 77?
FORTRAN is the dominant and most enduring programming language used in the history of computers. Many times experts have predicted that it will fade away with time, but those predictions have always failed due to its software inertia. Designing of bridges, airplane structures, storm drainage systems, analysis of scientific data, factory automation control, and many other such applications employ FORTRAN programming. Thousands of applications have been written in FORTRAN.
FORTRAN compiler and Editor
A FORTRAN program is typed and saved in an editor and then is converted into an executable file by a FORTRAN compiler. So, to start programming in FORTRAN, you need to download a FORTRAN compiler and an editor. Several free FORTRAN compilers are available on the internet. A simple editor such as NOTEPAD or DOS editor may be used. The crimson editor is also very good for writing FORTRAN programs in Microsoft windows. It takes up very little space with size being only 4MB and is user friendly. Having downloaded a compiler and an editor, you need to type a program in the editor and save the file with an extension.FOR to create a FORTRAN file. Then using a command prompt or command-line interface, you instruct the compiler to do its job.
Downloading The Compiler
- Create a folder named F in C drive. Download Fort99.zip by clicking the link and extract all the files directly in this folder i.e. all the content of this zip file must directly get extracted in this folder. 4 files are there in this zip file: G77, MINE, SLATEC, and York.
- Now, Download and editor (here, I will use crimson editor) and install it.
- Open a new file in the editor and paste your program in it. Save this program in the York directory in the F folder with extension.FOR followed by file name let’s say i.e. a.FOR
- Now, open the command prompt in your windows and type the following commands:
PATH=\F\G77\bin; SET LIBRARY_PATH=\F\G77\lib cd \F\York f2exe a a
f2exe is the execution command which is followed by the name of the file to be compiled.
Basic structure of a FORTRAN program
A FORTRAN program consists of 80 columns (lines) divided into 3 parts:
- Specification part
- Execution part
The first statement of a FORTRAN program is its heading. It marks the beginning of the program and specifies a name for the program. The heading is normally followed by opening documentation in the form of comments about the program’s input, output, and purpose. Letter ‘c’ or an asterisk causes the compiler to ignore the rest of the line which may therefore be used as a comment. The specification part includes names and types of constants and variables used to store input and output values and are included in columns 1-5. Column 6 is left blank intentionally. Executable FORTRAN commands are written in columns 7 to 72, beyond which columns are left unused.
Constants are the quantities whose value does not change during the execution of the program. Fortran supports 3 types of constants: numeric, character and logical and hence includes 6 data types:
- INTEGER– Data type INTEGER is used to store integers which may be preceded by a + or – sign. These are usually stored in 32 bits. Example: 123, +1, 0, -4 .
- REAL or single-precision– Data type REAL includes the number that may have a fractional part and occupy 4 bytes. Example: A REAL number 400 can also be written as 4E2 or 0.456 can be written as 456E-3.
- DOUBLE PRECISION- Double-precision constants are written in scientific notation with exponents being represented by D. For example 0.001 is written as 1D-3. Each double-precision value is stored in two consecutive memory locations.
- COMPLEX– In FORTRAN, a complex number a+ib is written as two real numbers in parantheses separated by comma (a,b)
- CHARACTER– Type CHARACTER includes a sequence of characters which may be uppercase alphabetic letters from A to Z, digits 0 to 9, blanks, and the following: +, -, *, =, /, (), ., $, ‘. It is often called a string and must always be delimited in single quotes (‘). Example: ‘JOHN P. RAY’. If an apostrophe is one of the characters, then it must be centered as a pair of apostrophes. Example: ‘DON” T’
- LOGICAL- This type is used to store and process logical data values: TRUE, FALSE
Variable is the name given to a word in Fortran. It is defined using one to six alphabetic characters and decimal digits, starting with an alphabetic character. Example: VOL TEMP COLUMN AIR1 Spaces are ignored by Fortran 77, however in Fortran 90, spaces become significant. For example: COL UMN is read as COLUMN by Fortran 77.
Each variable has a type depending on the type of constant it is meant to store. Type of the variable can be assigned to it in the following two ways:
- Explicit Typing
- Implicit Typing
In this type of the variable is specified with the help of a type specification statement. Type Variable_list
Here Type is the name of the type and Variable list is a single variable or a list of variables separated by commas.
Example: INTEGER WIDTH REAL NUM, VOL LOGICAL ERROR
FORTRAN does not compile type specifications into executable machine code instructions and hence are non- executable. We place them at the beginning of the program.
Using a variable without declaring it in type specification refers to as Implicit Typing. If the variables begin from any character from I to N, it is INTEGER type variable, otherwise, it is REAL. Example: TEMP is REAL but ITEMP is INTEGER.
Recommended: Declare the type of variables explicitly in the type specification. Avoid any implicit declaration and use explicit typing by giving IMPLICIT NONE command in the second column of the program.
A variable can only store a single value at a time. In order to store several values, subscripted variables are used that refers to a group of values (array) specified by a single name. This array can be of one dimension (vector), two dimensions (matrix) or a multi-dimension matrix with maximum of 7 dimensions. General form of an subscripted variable is a variable name followed by subscripts enclosed within parentheses.
For example: In u(j,k,l) , u is a sub scripted variable and j,k,and l are subscripts. Since there are 3 subscripts, it is a 3 dimensional array. Values of j, k and l will tell the number of elements in each dimension. In ITEM(3,5,6), ITEM is a 3-dimensional sub scripted variable, whose first dimension contain 3 elements, 2nd dimension contains 5 elements and 3rd dimension contains 6 elements.
One-dimensional subscripted variable
It is called a vector represents a linear sequence of elements stored consecutively in memory. Example: A(20), here, A is a subscripted variable with length 20. In FORTRAN, arrays are indexed from 1 upto end. So first number in the array A is denoted by A(1) and last number is denoted by A(20). In order to run the index from 0, one needs to write For example: A(0:19), here, index runs from 0 to 19 and length of array is 20 as before.
Two dimensional subscripted variable
This is used to represent matrices. Example A(3,5), here, first entry shows number of rows and second entry shows number of variables. The array formed has 3*5=15 subscripts or say numbers. This command gives us a graphical picture (1,1),(1,2),(1,3),(1,4),(1,5) (2,1),(2,2),(2,3),(2,4),(2,5) (3,1),(3,2),(3,3),(3,4),(3,5) And similarly multidimensional arrays may be defined.