Fortran
Fortran
Fortran
DO 120 K = 3, 10, 2
PRINT *, “K= “, K
120 CONTINUE
What prints?
EXAMPLE DO
DO 120 K = 3, 10, 2
PRINT *, “K= “, K
120 CONTINUE
What prints?
K= 3
K= 5
K= 7
K= 9
ARITHMETIC OPERATORS
* multiplication
/ division
+ addition, unary positive
- subtraction, unary negative
** exponentiation
REAL ARITHMETIC
Providing all variables and constants in the
expression are real, real arithmetic will be
carried out as expected, with no decimal
places being truncated.
INTEGER ARITHMETIC
Providing the expression has all integers,
subtraction, addition, multiplication and
exponentiation will prove no problem.
However integer division is somewhat
different than normal division with real
values. Integer division ignores the
fractional part. Any decimal places are
truncated.
MIXED-MODE
ARITHMETIC
Mixed mode arithmetic is when an
expression contains both reals and
integers. If ANY of the operands are real
then result of the operation will be real.
However, mixed mode arithmetic should
be used with extreme care. You may think
you have a real operand when in reality
you have two integer operands.
MIXED MODE EXAMPLES
5 / 2 * 3.0 =
MIXED MODE EXAMPLES
5 / 2 * 3.0 = 6.0
3.0 * 5 / 2 =
MIXED MODE EXAMPLES
5 / 2 * 3.0 = 6.0
3.0 * 5 / 2 = 7.5
ARITHMETIC FUNCTIONS
ABS absolute value MIN min of a list
COS cosine MOD int division
DBL double NINT round to int
DPROD dp product REAL real
EXP exponentiation SIN sine
INT integer SQRT square root
LOG logarithm
MAX max of a list
Input Output Statements
READ
WRITE
PRINT
FORMAT CONTROLS IN SEPARATE
STATEMENT
UNIT NUMBERS TO DIRECT IN/OUT
UNFORMATTED * FOR EASE OF USE.
READ * / PRINT *
ASSUMES A UNIT (KEY BOARD)
ASSUMES A FORMAT (TYPE CONTROLLED)
E.G.
INTEGER A,B,C
READ * A,B,C
PRINT * A,B,C
1 13 15 input
1 13 15 output
READ * / PRINT * (cont.)
INTEGER A,C
REAL B
READ * A,B,C
PRINT * A,B,C
12 4.5 -18 input
12 4.5 -18 output
Pretty straight forward!
Formatted I/O
Add a FORMAT statement
Usually in a separate statement
I = 128
PRINT 9000, I
9000 FORMAT(1X, ‘I = ‘, I8)
I= 128 output
Format Codes
A character string P scaling factor
B blank handling S sign control
D double precision T tabbing control
E single precision X horizontal space
F single precision Z hexadecimal
G generic (d,f,i,…
/ vertical skipping
H literal strings
I integer
‘ enclosed literal
L logical values
: termination
Some Examples
100 FORMAT(1X, 3I5)
200 FORMAT(1X, F6.2, E12.5)
First character is form control
‘ ‘ (blank) skip one line before printing
‘1’ (one) skip one page before printing
(the 8 inch listing story)
‘0’ (zero) double space
Spacing
Depends on the type of conversion.
E.g. A format
If the field width is shorter than the number of
characters stored in the variable, characters
are truncated on the right.
If the field width is longer than the number of
characters stored in the variable, the field is
padded on the left with blank characters.
Sub Programs
SUBROUTINE SAMPLE(R,S,X,Y)
INTEGER R,S
REAL X,Y
… body of subroutine
RETURN
END
Function
INTEGER FUNCTION TEST(T,U,V)
REAL T,U
LOGICAL V
… body of function
TEST = 1 + 2
RETURN
END
More On Arrays
INTEGER ARY(1:10)
INTEGER BARY(-5:5)
REAL*4 ARY2D(1:4,1:5)
REAL*8 MYARY(-2:2,-4:4)
INTEGER CUBE(1:3,1:5,1:10)
INTEGER MYCUBE(-3:2,-4:2,-3:10)
Passing Arrays As
Parameters
INTEGER X(10) SUBROUTINE SUB1(A)
CALL SUB1(X) INTEGER A(10)
A(1) IS SAME AS X(1)
Passing Arrays As
Parameters
INTEGER X(10) SUBROUTINE SUB2(B)
CALL SUB2(X) INTEGER B(7)
B(1) IS SAME AS X(1)
CAN REFERENCE FIRST
7 ELEMENTS OF X.
Passing Arrays As
Parameters
INTEGER X(10) SUBROUTINE SUB3(C)
CALL SUB3(X(3)) INTEGER C(4)
C(1) IS SAME AS X(3)
CAN REFERENCE 4
ELEMENTS OF X.
Passing Arrays As
Parameters
INTEGER R(2,3) SUBROUTINE SUB4(T)
CALL SUB4(R) INTEGER T(2,3)
T(1,1) IS SAME AS
R(1,1)
CAN REFERENCE ALL 6
ELEMENTS OF R.
Passing Arrays As
Parameters
INTEGER R(2,3) SUBROUTINE SUB5(U)
CALL SUB5(R) INTEGER U(3,2)
U(1,1) IS SAME AS
R(1,1)
CAN REFERENCE ALL 6
ELEMENTS OF R.
U(1,1)=R(1,1)
U(2,1)=R(2,1)
U(3,1)=R(1,2)
U(1,2)=R(2,2)
U(2,2)=R(1,3)
U(3,1)=R(2,3)
Passing Arrays As
Parameters
INTEGER R(2,3) SUBROUTINE SUB6(V)
CALL SUB6(R(1,2)) INTEGER V(2)
V(1) IS SAME AS R(2,1)
V WILL REFERENCE
THE 2ND COLUMN OF
ARRAY R.
V(1)=R(1,2)
V(2)=R(2,2)
Array Name Used Without
Subscript
In an argument or parameter list
In COMMON or type statement
In an EQUIVALENCE statement
In a DATA statement
In the list of an input or output statement
As the unit identifier for an internal file in
an input or output statement
As a format identifier in an input or output
statement
Assigned GOTO
ASSIGN stno TO name
GOTO name, (stno1, stno2, … stnok)
Call Subroutine
CALL name (argument list)
Common Statement
COMMON /common name/ common list
COMMON /SAM/ A,B,C
Same statement in main program and in
subroutines.
Two names for the same thing.
COMPLEX
Built in to FORTRAN
COMPLEX*8 LIST
COMPLEX*16 LIST
Implied Do Loop
Used in I/O
READ *, N, (PRIME(I), I=1,N)
WRITE *, (PRIME(J),J=1,3)
ELSEIF
IF (logical expression) THEN
statements s1
ELSEIF (logical expression) THEN
statements s2
ELSE
statements s3
ENDIF
Equivalence
EQUIVALENCE (A,B)
Alias naming.
INTEGER A(4),B(2),C(2)
EQUIVALENCE (A(1),B(1)),(A(3),C(1))
B(1) A(1)
B(2) A(2)
C(1) A(3)
C(2) A(4)
A Last FORTRAN Story
A Last FORTRAN Story
COMMON Base
Grows
A Last FORTRAN Story
COMMON Base
At top of memory!
Grows
A Last FORTRAN Story
Address Wrap Around
COMMON Base
Grows
A Last FORTRAN Story
HALT!
(00007) Program
Counter (instruction
address register)
COMMON Base
Grows
FORTRAN 2000 ? Or F?
http://www.fortran.com/F/
http://www.fortran-2000.com