FORTRAN use of columns
C in column 1 was usual
* in column 1
! in column 1 for
Lahey FORTRAN
numbers in columns 1 through 5
entry in column 6
use an upper case C
columns 7 through 72
columns 73-80
in any columns
Data types
INTEGER
·
Variable
Names which begin with the letters I N are by default, integer variables.
·
Integer
variables beginning with these letters do not have to be declared
REAL
·
Variable
names which begin with any
letter other than I - N
are by defaults, real.
·
Real
variables beginning with any letter other than I - N do not have to be declared
LOGICAL
·
Logical
variables must be declared
·
Logical
values are .TRUE. .FALSE.
COMPLEX
·
Complex
variables must be declared
·
Complex
variables have a real and an imaginary part
·
Complex
C - is an example declaration
·
C =
COMPLX (A,B) will create a complex
number C = A + iB where i =
the square root of -1
DOUBLE
PRECISION
·
Double
precision variables must be declared
CHARACTER
·
Type
used for holding Strings
Data Structure -
ARRAY
Array Declaration examples
DIMENSION
A(3)
·
declares
a one dimensional array with 3 elements
·
subscripts
start a 1
·
array
A has 3 locations A(1), A(2), A(3)
DIMENSION
X(4,5)
·
declares
a two dimensional array with 4 rows and 5 columns
·
array
X has 20 locations in 4 rows and 5 columns
OPERATORS
Arithmetic
operators
Relational
operators
·
.EQ.
·
.NE.
·
.GT.
·
.GE.
·
.LT.
·
.LE.
Logical
operators
·
AND
·
OR
·
NOT
Assignment operator
·
=
FORTRAN Statements
Non-executable
END
·
stops
the compilation process of a unit
FORMAT descriptors
·
L for
Logical
·
F ...for fixed point
·
I for integer
·
E ...for exponential data
·
F for complex
·
D for double precision
·
H for characters
CONTINUE
·
non-executable
statement
·
generally
used to mark the end of a DO loop and to affix the label to
DATA
·
used
to specify the initial values of certain variables at compile time
·
can
not be used during program execution (run time)
·
example: DATA X/2.16/,Y/31.8/,I/26/
CARRIAGE CONTROL CHARACTERS for output
·
1x a blank space
·
(a
blank space) - advance 1 line (i.e. newline, or single space)
·
0 advance
2 lines (i.e. double space)
·
- advance 3 lines - not all compilers
·
1 advance to top of next page
·
+ do not advance to a new line (i.e.
overstrike)
Executable
1. STOP
statement
stops the execution of the program
can occur anywhere in the program
can occur in more than one place, the one
reached first stops the program
its not good form to have multiple stop
statements
2.
ASSIGNMENT statement
variable = expression
3.
CONDITIONAL
·
3
types of selection statements
Example 1 (form and code) IF statement
IF (Boolean
condition) DOTHIS
IF (X .GT. O.O) GO TO 7
NUMNEG = NUMNEG+ 1
GO TO 8
7
NUMPOS
= NUMPOS + 1
8
ICOUNT = ICOUNT + 1
Example 2 (form, explanation and code) - ARITHMETIC IF
If (arithmetic value) label, label2, label3
·
goes
to statement labeled 25 if I < 0;
·
goes
to statement labeled 365 if I = 0;
·
goes
to statement labeled 43 if I > 0
IF (I) 25, 365, 43
Example 3 (form, explanation and code) - COMPUTED GO TO
GO T0 (label1, label2, label3, label4) , variableName
·
goes
to statement labled 23 if J = 1;
·
goes
to statement labeled 45 if J = 2,
·
goes to statement labeled 73 if J = 3; etc.
GO TO
(23,45,73,98 104), J
Note:
examples 2 and 3 are scheduled to be DEPRECATED
4. INPUT
statement (form and code)
READ (5,label) list of variables to be read
·
The 5
specifies the default input device. The
label refers to the associated format statement
READ (5, 12) X,I
12 FORMAT (F2.1, I3)
5.
OUTPUT statement
(form and code) - more below
The
6 specifies the default output device.
The label refers to the associated
Format statement. The
F in F4.1 specifies that its a fixed point number; the I in
I3
specifies that it is an integer. The
integer following the F and the I specifies
the number of columns to be used.
The .1 in F4.1
specifies that only one place
after the decimal point is to be shown
WRITE
(6, label ) list of variables to be printed
WRITE (6,13) X,I
13
FORMAT
(1X, F4.1,I5)
6.
LOOP statement (form
and code)
DO label variableName = startvalue,
endvalue, modificationValue
label CONTINUE
DO 10 M = 5, 13, 1
A = 3 * M
WRITE (6, 59) A
10 CONTINUE
7. RETURN
statement
·
Returns
from functions or subroutines (the 2 kinds of subprograms)
·
The
return statement is an executable statement
8.
Subprograms
·
FORTRAN
has three types of subprograms:
statement functions, subroutines and functions.
·
Functions
return a single value by assignment to the function name.
·
Subroutines
return zero, one or many values through the parameter list.
·
FORTRAN
parameters are passed by reference (address)
·
The
code for functions and subroutines subprograms should physically follow the
code for the main program
Statement functions
·
have
the general form: name (a1, a2 . ..... an)
= F(a1, a2
. ..... an)
·
the parenthesized variables on the left side represent the
arguments to the function.
·
The
entire right side is an expression which specifies the value of the function
P(X) = (A*X + B)*X + C
Subroutines can be parameterless, or have parameters
·
Here
is an example of a parameterless subroutine.
SUBROUTINE
PRINTHI
WRITE (6, 10)
10
FORMAT (1X, HI )
RETURN
END
·
The
calling statement in the main program for the above subroutine would b
·
Note
that the word subroutine does NOT appear in the call statement
CALL PRINTHI
·
Below
is the skeleton for a subroutine with parameters.
·
By
default, the values this subprogram expects to receive are real (i.e. fixed
point) values.
·
If you
intend to pass in integers you must add a line directly following the subroutine declaration which declares X,Y, and Z as integers (i.e.
INTEGER X,Y,Z)
·
Unlike
Java, changes that are made to the formal parameters (inside the subroutine)! are reflected (affect) the actual (calling) parameters.
SUBROUTINE
SORXYZ (X, Y, Z)
...
...
RETURN
END
Functions
·
FORTRAN
functions return a single value and return it by assigning the value to the
function name (inside the function).
Recursion was
not available in early
FORTRAN and we will not be using it in our FORTRAN programs.
·
By
default, function names indicate the type of the return value.
·
The function below finds the
sum of the three numbers X, Y, and Z which are assumed to be real (floating
point) numbers.
·
If you
want them ! to be integers, you must declare them as integers inside the
function.
FUNCTION SUM (X,Y,Z)
SUM = X + Y + Z
RETURN
END
·
To call a function you simply write the function
name followed by its actual
parameters in parentheses.
·
The returned value replaces the call and you should store the
returned value somewhere.
·
Below is an example.
·
Note that the returned SUM replaces the right hand
side of the assignment
statement and is stored in the variable ANSWER on the left of the
assignment operator.
ANSWER = SUM (A,B,C)
Exception
handling statements
·
END = label
·
ERR = label
Graduate Students
COMMON statements
IMPLICIT statements
EQUIVALENCE statements