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.fcompile source files into executable named NAME- program code must start on 7th column (for historical reasons)
- comments start with
C
INTEGERstandard signed integer, can have +/- in frontREALsigned floating point numbers, can use scientific notation eg. 6.0123E+23CHARACTER*nstring of character enclosed by single quotes must specify n, which is length of string when declaring
-
PRINT *, listprints each of the values in the list of a line -
READ *, listplaces data into list from the data line (user input) -
ENDtells 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 se1 and e2 are expressions, both of the same typerelis relational operatorsis 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
IFand 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 *, NCode above loops ten times. After
DOthe first value is line number after which the loop goes to the next iteration.Nis the iterator that increases or deacreases, top is ending value and last integer is increment. -
CONTINUEstatement 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 IFstructure. AddTHENafter eachELIF -
.EQV.equivalence operator, used to compare boolean values -
.NEQV.nonequivalence operator -
May omit
ELSEinIF-THENstatement. -
LOGICALdata type to hold booleans -
Use
INT(x)to truncate real numberxorNINT(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).STRis string,jandkare 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 // CconcatenatesBwithCand 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
RETURNand `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
PRINTstatement by replacing*by line whereFORMATis stated:PRINT 10, SNGLSP, 'STRING' 10 FORMAT(A,T25, A)This will print the text
STRINGin 25th column. -
SNGLSPis carriage control character that can be the following:move down the page one line before printing0move down two lines1move to the top of next page+overprint previous line
-
Looks like
SNGLSPis not needed when printing to terminal, so this is enough:PRINT 10, 'STRING' 10 FORMAT(T25, A) -
Different data descriptors:
Afor chars,Ifor ints,Ffor reals (I4orF6.2)
OPEN(speclist)open files with this commandCLOSE()disconnect fileREAD/WRITE()read or write files