A Small Fortran 77 Guide

Öğr.Gör.Ahmet Bingül
bingul@gantep.edu.tr


Books and Guides

Books

Guides Other Resources

/\ Top

Writing Programs

Writing, Compiling, and Executing Fortran 77 programs (there is a very detailed guide on this section in gul-fortran77.doc) You need a gul1 or, gul2 account, and the commands are:

   $ edit myprogram.f
   $ fortran myprogram.f
   $ run myprogram
assuming your Fortran source is in the file myprogram.f. The command fortran myprogram.f compiles the Fortran source file myprogram.f to the executable file myprogram.

/\ Top

Reading from and Writing to Standard I/O

Unformatted PRINT Statement

One can write anything into standard output via the unformatted PRINT statement. General form is:

              PRINT *,list items
The list items of this statement, separated by commas can contain: Example: Printing a message
              PRINT *,'Hello World'
will output
              Hello World
Example: If an apostrophe is wanted to be printed in a message,
              PRINT *,'Ahmet''s book'
will output
              Ahmet's book
Example: If A=2.0, B=5.0 then
              PRINT *,'A=',A,'B=',B
              PRINT *,(A+B)/2
will output two lines
              A=3.0 B=5.0
              4.0

Unformatted READ Statement

One can read a value from standard input via the unformatted READ statement. General form is:


              READ *,Input items
Input items which must be separated by commas can contain Example: reading two real variables A and B form keyboard
              READ *,A,B
The input record for these two variables may be provided in the following ways: Example Programs program00.f | program01.f |

/\ Top

Data Types

Types of variables are INTEGER, REAL, COMPLEX, CHARACTER, LOGICAL
Example Programs program02.f | program03.f | program04.f

/\ Top

Operators

More Operators | Priority of Operators

Arithmetic operators are listed in Table 1.

Table 1 : Arithmetic Operators
Operator Explanation Example Meaning
+ Addition x+y addition of x and y
- Substraction x-y diffrence of x and y
* Multiplication x*y multiplication of x and y
/ Division x/y ratio of x and y
** Take power x**y power of x to the y

Example Programs: program05.f | program06.f | program07.f

/\ Top

Intrinsic Functions, I/O

More Intrinsic Functions

Intrinsic Functions are ABS(X), LOG(X), EXP(X), SIN(X) etc.
Example Programs: program08.f | program09.f

/\ Top

Control Statements

In FORTRAN, the IF statement is a conditional branching statement; it can be used to evaluate conditions as well as to make a decision whether the block of code controlled by the statement is going to be executed.

Using IF

            IF(expression) THEN

              a list of statements

            END IF

Here expression is the conditional criterion. If expression is logical TRUE (that is, nonzero), the statements inside IF and END IF are executed. If expression is logical FALSE (zero), then the statements are skipped.

Using IF-ELSE

As an expansion of the IF statement, the IF-ELSE statement has the following form:


            IF(condition) THEN
              list of statements
            ELSE
              otherwise
            END IF

Here the if condition is logical TRUE, the statements controlled by IF , including list of statements are executed. The statements, otherwise , inside the statement block and following the ELSE keyword are executed IF condition is not logical TRUE.

Using block IF-ELSE IF ... ELSE

As you saw in the previous sections, one IF statement enables a program to make one decision. In many cases, a program has to make a series of decisions. To enable it to do so, you can use nested or block IF statements, like this:


            IF(condition1) THEN
              statements for condition1
            ELSE IF(condition2)
              statements for condition2
            ELSE IF(condition3)
              statements for condition3
            .
            .
            .
            ELSE IF(conditionN)
              statements for conditionN
            ELSE
             otherwise 
            END IF
Example Programs: program10.f | program11.f

/\ Top

Repetitive Structures (Iteration)

Using DO - ENDDO

This is an executable FORTRAN statement. The general form of the DO ... ENDDO statement is:


            DO var = exp1, exp2, exp3

                   list of statements

            END DO
Notes: CONTINUE Statement
This is an executable FORTRAN statement. But it does not do anything. It can used anywhere as a convenient for a label stn .

            DO stn var = exp1, exp2, exp3

                   list of statements                   

stn         CONTINUE
Using Nested Loops

You can put a loop inside another one to make nested loops. The computer will run the inner loop first before it resumes the looping for the outer loop. For example:

      ...
      DO 10 I=1,10
        DO 20 J=1,10
             MUL = I*J 
             PRINT *,MUL
20      CONTINUE
10    CONTINUE
      ...
Example Programs: program12.f | program13.f | program14.f | program15.f | program16.f | program17.f | program18.f

/\ Top

Arrays

We know how to declare a variable with a specified data type, such as CHARACTER , INTEGER , REAL , or COMPLEX . In many cases, we have to declare a set of variables that have the same data type. Instead of declaring them individually, FORTRAN allows us to declare a set of variables of the same data type collectively as an array.

An array is a collection of variables that are of the same data type. Each item in an array is called an element. All elements in an array are referenced by the name of the array and are stored in a set of consecutive memory slots in a PC.

Declaring Arrays and DIMENSION Statement

The DIMENSION statement and other type decleration statements is placed at the beginning of the program before any executable statement. The DIMENSION statement specifically:

and its general form is:

   DIMENSION A(10), B(2,3), ISUM(50), X(3,2,5)
The following is also the general form to declare an array:
        data-type  ArrayName(ArraySize)

Here data-type is the type specifier that indicates what data type the declared array will be. ArrayName is the name of the declared array. ArraySize defines how many elements the array can contain. Note that, the paranthesis ( and ) are required in declaring an array. The paranthesis pair ( and ) is also called the array subscript operator.

For example, an array of integers and real are declared in the following statements:

       INTEGER A(8)
       REAL    MASS(10)
Example Program: program19.f | program20.f |

/\ Top

Formatted I/O

Input and output in a program are performed by means of unformatted READ and PRINT statements. These are also called
free format input and ouput statements. Most of the time, a user wants to specify the form of data in an input record and design the form of output lines on a printer or screen. If this is the case, then user should use formatted input and output statements.

The Formatted Input and Output Statements

For the screen, the formatted input and output statements are:


       READ  fmt,list of items         ! input
       PRINT fmt,list of items         ! output
       READ  (unt,fmt) list of items   ! input
       WRITE (unt,fmt) list of items   ! output

where unt is the reference to an I/O unit which will be explained later and fmt is a required format identifier which can be:

The FORMAT Statement

The FORMAT statement is used with the above I/O statements to specify the stucture and the form of the data fields within the record. Its general form is:


       fmt  FORMAT (format codes)

where fmt is the statement number given in the I/O statements and format codes are the specifications of the releted list items. Some most useful format codes are listed in Table 2.

Note: FORMAT statement can be put anywhere before the END statement in a program unit.

Table 2 : Format Codes
Code Form Description
I rIw Integer data fields
F rFw.d Floating-Piont data fields
E rEw.d Exponential data fields
D rDw.d Double precision data fields
A rAw Character data fields
X nX Skips n columns in an iput record or leaves n blanks on an input record

Here Example: Assume that the following input record is given.
 23-796789-157        
----------------------
10 FORMAT(I3,4x,I3,2I4) 20 FORMAT(I4,I3,I4,2x,I3) ... READ 10,K1,K2,K3,K4 ! will assign: K1=23, K2=789, K3=-157, K4=0 PRINT 20,K1,K3,K4,K2 ! will print : 23*** 0 789
Example: Consider the following input record.
36725  3.6725-36725 2.32
------------------------
the record can be input as follows:
 
2       FORMAT(F5.2,F8.4,F6.5,F5.1)
7       FORMAT(F5.6,F5.3,F6.3,F2.1)
        ...
        READ  2,X1,X2,X3,X4   ! will assign: X1=367.25, X2=3.6725, X3=-0.36725, X4=2.32
        PRINT 7,X1,X4,X3,X2   ! will print : 367.32.320-0.367** 
Example Programs : program21.f | program22.f

/\ Top

File Management

In FORTRAN, a file represents a device with which you want to exchange information. Before you perform any communication to a file, you have to open the file. Then you need to close the opened file after you finish exchanging information with it.

Opening and Closing a Disk File

OPEN() and CLOSE() functions are used to open and close a file respectively. General form to use these function is:


         OPEN(UNIT=unt, FILE=fnm, STATUS=sts)
         ...
         CLOSE(unt)
Here Infact also, there are a few parameters of the functions OPEN and CLOSE .

Example: This Fortran section, opens a file having name ' test.dat ' to print the numbers 1,...,10 and their squares into that file.


         ...
         OPEN(UNIT=1, FILE='test.dat', STATUS='new')
	 DO I=1,10
           WRITE(1,20) I,I*I
         END DO
         CLOSE(1)
         ...
20       FORMAT(I2,2x,I3)
         ...

Example: Consider we have a file ( xy.dat ) like this:


0.45  10.62512
1.38  12.154556
2.77  13.001054
3.26  14.2262
4.13  14.99569
5.41  17.215999
6.88  19.87745
7.97  22.45032
8.15  29.861418
9.88  39.3521
We want to get the average value of Xs and Ys. This fortran section is used to do this.
         ...
         TotalX = 0.0
         TotalY = 0.0
         ...
         OPEN(UNIT=1, FILE='xy.dat', STATUS='old')
	 DO I=1,10
           READ(1,*) X,Y
           TotalX = TotalX + X
           TotalY = TotalY + Y
         END DO
         CLOSE(1)
	 AvrX = TotalX / 10.
	 AvrY = TotalY / 10.
	 PRINT *,'Average of Xs is :',AvrX
	 PRINT *,'Average of Ys is :',AvrY
         ...

Example Program : program23.f | program23.data | program23.grades |

/\ Top

Subprograms: Functions and Subroutines

Sometimes the program gets larger and larger because of repeating code, it really becomes difficult to follow and trace. When the repeating processes become unavoidable in a program, it is shortened by writing the repeating part as a sperate program that is called as a subprogram and can be refered to whenever necessary in a suitable calling statement.

FUNCTION

A FUNCTION type subprogram is a seperate and complete FORTRAN program consisting of a number of source statements. Its general form is:


     data-type FUNCTION name(list of argument)
     ...
     name = an expression
     ...
     RETURN
     END
Notes: Example Function:

     INTEGER FUNCTION SUM (A,B)
     SUM = A+B
     RETURN
     END
In the above section, SUBROUTINE

The SUBROUTINE type subprogram is also a separate program. Its general form is:


     SUBROUTINE name(list of arguments)
     .
     .
     .
     RETURN
     END
Notes:

For most of sample subroutines, you can download the file subroutines.doc

Example Subroutine:

     SUBROUTINE SUM (A,B,RESULT)
     RESULT = A+B
     RETURN
     END
In the above section,

The SUBROUTINE type sub programs are called in a separate CALL statement in an any other SUBROUTINE s, FUNCTION s or main program. General form of CALL statement is:

       CALL name(list of actual arguments) 

where name is the name of the SUBROUTINE list of actual arguments which can be constants, a variable, array names, array elements, expression or combination of these. For example

       CALL SUM(1.0,-5.0,ADD) 
if one can print the variable ADD , it will be -4.0 .

Example Programs for FUNCTION : program24.f | program25.f | program26.f | program27.f

Example Programs for SUBROUTINE : program28.f | program29.f | program30.f | program31.f

/\ Top

Other Topics in FORTRAN

/\ Top


Home | E-mail
Last Modified:Dec-11-2008