Allegheny College
Department of Computer Science
CS220 Programming Languages
Fortran 77
[Code Sample 1]
program circle
real r, area
c This program reads a real number r and prints
c the area of a circle with radius r.
write (*,*) 'Give radius r:'
read (*,*) r
area = 3.14159*r*r
write (*,*) 'Area = ', area
stop
end
[End of Code Sample 1]
Fortran 77 is not a free-format language, but has a very strict set of rules for how the source code should be formatted. The most important rules are the column position rules:
Col. 1 : Blank, or a "c" or "*" for comments
Col. 2-5 : Statement label (optional)
Col. 6 : Continuation of previous line (optional)
Col. 7-72 : Statements
Col. 73-80: Sequence number (optional, rarely used today)
Most lines in a Fortran 77 program starts with 6 blanks and ends before column 72, i.e. only the statement field is used.
c23456789 (This demonstrates column position!)
c The next statement goes over two physical lines
area = 3.14159265358979
+ * r * r
$g77 filename.for -o filename
$./filename
integer list of variables... (var1, var2, var3....)
real list of variables
double precision list of variables
complex list of variables
logical list of variables
character list of variables
When different data types occur in the same expression, type conversion has to take place, either explicitly or implicitly. Fortran will do some type conversion implicitly. For example,
real x
x = x + 1
will convert the integer one to the real number one, and has the desired effect of incrementing x by one. However, in more complicated expressions, it is good programming practice to force the necessary type conversions explicitly. For numbers, the following functions are available:
int
real
dble
ichar
char
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:
w = dble(x)*dble(y)
Note that this is different from
w = dble(x*y)
Logical expressions can only have the value .TRUE. or .FALSE..
A logical expression can be formed by comparing arithmetic expressions
using the following relational operators:
.LT. meaning (less than)
.LE. (less than or equal to)
.GT. (greater than)
.GE. (greater than or equal to)
.EQ. (equal to)
.NE. (not equal to)
Logical expressions can be combined by the logical operators .AND. .OR. .NOT.
Example of simple if-statement
if (logical expression) then
statements
endif
Example of If-elseif-else block
if (logical expression) then
statements
elseif (logical expression) then
statements
:
:
else
statements
endif
Example of nested if-elseif block
if (x .GT. 0) then
if (x .GE. y) then
write(*,*) 'x is positive and x >= y'
else
write(*,*) 'x is positive but x < y'
endif
elseif (x .LT. 0) then
write(*,*) 'x is negative'
else
write(*,*) 'x is zero'
endif
The do-loop is used for simple counting:
integer i, n, sum
sum = 0
do 10 i = 1, n
sum = sum + i
write(*,*) 'i =', i
write(*,*) 'sum =', sum
10 continue
The general form of the do loop is:
do label index = initial_value, terminating_value, step_size (implicityly = 1)
statements
label continue
basic format:
label if (logical expr) then
statements
goto label
endif
example:
integer n
n = 1
10 if (n .le. 100) then
n = 2*n
write (*,*) n
goto 10
endif
basic format:
label continue
statements
if (logical expr) goto label
The one-dimensional array, which is just a linear sequence of elements stored consecutively in memory. Which can be declared by the following line:
real array_name(20)
Array elements are indexed from array_name(1) through array_name(20).
Two dimensional arrays can be declared by:
data_type array_name(number_of_rows,number_of_columns)
example:
real A(3,5)
The above statement declares a two-dimensional array of 3 rows and 5 columns, or 3 X 5 = 15 real numbers.
This data structure can be easily visualized through the following table:
(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)
Two-dimensional arrays may also have indices in an arbitrary defined range. The general syntax for declarations is:
array_name (low_index1 : hi_index1, low_index2 : hi_index2)
The total size of the array is then
array_size = (hi_index1 - low_index1+1)*(hi_index2 - low_index2+1)
abs absolute value
min minimum value
max maximum value
sqrt square root
sin sine
cos cosine
tan tangent
atan arctangent
exp exponential (natural)
log logarithm (natural)
example: x = cos(pi/3.0)
A fortan function has to be declared with the correct type in the calling program unit. The function is then called by simply using the function name and listing the parameters in parenthesis. A function returns only 1 value.
example:
program main
real function_name, t, sum
integer m
read (*,*) t
sum = 0.0
do 10 m = 1, 12
sum = sum + funtion_name(m, t)
10 continue
write (*,*) 'Annual rainfall is ', sum, 'inches'
stop
end
real function function_name(m,t)
integer m
real t
funtion_name = 0.1*t * (m**2 + 14*m + 46)
if (function_name .LT. 0) function_name = 0.0
return
end
Fortran Functions:
A function in Fortan is used to return 1 and only 1 value. However subroutines can be used to return 0 or more values.
Read is used for input, while write is used for output.
read (unit_number, format_number) list-of-variables
write(unit_number, format_number) list-of-variables
unit_number --> Will refer to standard i/o or a file
fomrat_number --> Will refer to a label or format statement
list-of-variables --> a comma delimited list of variables.
example using standard i/o:
read (*,*) list-of-variables
write(*,*) list-of-variables
or alternatively you can use the following format to read/write from standard i/o:
read *, list-of-variables
print *, list-of-variables