Fortran is a programming language that was invented back in 1954 by IBM. Fortran is faster and more efficient than any other programming language, making it well-suited for tasks such as scientific simulations and large-scale numerical modeling.
These notes are based on the book "FORTRAN 77 for Humans" written by Rex L. Page and Richard L. Didday and published in 1980.
g95 -o NAME SOURCE.f
compile source files into executable named NAME- program code must start on 7th column (for historical reasons)
- comments start with
C
INTEGER
standard signed integer, can have +/- in frontREAL
signed floating point numbers, can use scientific notation eg. 6.0123E+23CHARACTER*n
string of character enclosed by single quotes must specify n, which is length of string when declaring
-
PRINT *, list
prints each of the values in the list of a line -
READ *, list
places data into list from the data line (user input) -
END
tells the compiler to stop doing statements in program -
Operations are
+
,-
,*
,/
,**
(last is exponentation) -
If your line is too long, then put
&
in 6th column of next line:PRINT *, 'THIS LONG LINE IS', &'CONTINUED HERE'
-
IF (e1 rel e2) GOTO s
e1 and e2 are expressions, both of the same typerel
is relational operators
is statement label (line number) decide if relationship is true, if yes proceed to lines
-
Relational operators:
.LT.
less than.LE.
less than or equal to.EQ.
equal to.NE.
not equal to.GE.
greater than or equal to.GT.
greater than -
indent if loop blocks, so starting after
IF
and going until lastGOTO
:100 IF (test for exit) GOTO 200 fortran operation . . GOTO 100 200 continue program
-
can do while loops (if statement at beginning) or do-while (if statement end)
-
pre-test loops are safer and clearer
-
Do loops are like for loops:
INTEGER TOP = 5 DO 10 N=1, TOP,1 10 PRINT *, N
Code above loops ten times. After
DO
the first value is line number after which the loop goes to the next iteration.N
is the iterator that increases or deacreases, top is ending value and last integer is increment. -
CONTINUE
statement that does nothing, used as terminal statement in the range ofDO
-loops -
Nested loops are possible. Note that both line statements point to line 10:
DO 10 I=1, 10, 1 DO 10 J=1, 10, 1 10 PRINT *, I*J
-
If-else structure in fortran:
IF (test condition) THEN fortran operation 1 ELSE fortran operation 2 ENDIF
-
Can also use multiple else-if with
ELSE IF
structure. AddTHEN
after eachELIF
-
.EQV.
equivalence operator, used to compare boolean values -
.NEQV.
nonequivalence operator -
May omit
ELSE
inIF-THEN
statement. -
LOGICAL
data type to hold booleans -
Use
INT(x)
to truncate real numberx
orNINT(x)
to round -
MOD(x, n)
is modulus operator
-
INTEGER ARR(n)
declare an array of length n -
ARR(1)
access first element (indexing starts from 1 not 0) -
MUL(3,3)
to create 2-dimensional array. first row, then column -
To loop arrays use the following structure (reads 20 ints from user):
DO 100 I=1,20 READ *, ARR(I) 100 CONTINUE
-
Substrings can be accessed with
STR(j:k)
.STR
is string,j
andk
are indices -
multiline code can be written with
+
sign in between:IF (PHRASE .EQ. 'HECK' + .OR. PHRASE .EQ. 'DARN')
-
when assigning substrings, can't reference same portions eg.
N(1:9) = N(5:15)
-
INDEX('ABCDEF', 'C')
operator returns 3 as a result. Returns 0 if not found. -A = B // C
concatenatesB
withC
and stores it inA
- Fortran uses subroutines and functions
CALL AVG(a1, a2)
calls subroutine AVG with arguments a1 and a2SUBROUTINE AVG(a1)
declaration of subroutine- Subroutines must end with
RETURN
and `END statements - Function returns one value, subroutines can return any number of values through it's arguments. Choosing which to use is matter of taste.
type FUNCTION f(p1)
declares function f that takes in one parameter returns variable with name f at RETURN statement- first write main program, then function/subroutine definitions
-
Format
PRINT
statement by replacing*
by line whereFORMAT
is stated:PRINT 10, SNGLSP, 'STRING' 10 FORMAT(A,T25, A)
This will print the text
STRING
in 25th column. -
SNGLSP
is carriage control character that can be the following:0
move down two lines1
move to the top of next page+
overprint previous line
-
Looks like
SNGLSP
is not needed when printing to terminal, so this is enough:PRINT 10, 'STRING' 10 FORMAT(T25, A)
-
Different data descriptors:
A
for chars,I
for ints,F
for reals (I4
orF6.2
)
OPEN(speclist)
open files with this commandCLOSE()
disconnect fileREAD/WRITE()
read or write files