Science makes it known,
Engineering makes it work,
Art makes it beautiful.
|
|
Structure and Elements of a FORTRAN Program
Fixed Format:
| column |
contents |
| 1 |
"c" or "*" for a comment line |
| 1-5 |
statement number or label (non-signed integer) |
| 6 |
blank or character for continuation line |
| 7-72 |
Fortran statement |
| 73-80 |
sequence number (usually omitted) |
Form of a FORTRAN Program
PROGRAM <program-name>
{<specification-statements>}
{<executable-statements>}
END
|
Form of a FORTRAN Subroutine
SUBROUTINE <subroutine-name>[<dummy-argument-list>]
{<specification-statements>}
{<executable-statements>}
RETURN
END
|
Form of a FORTRAN Function
[<type>] FUNCTION <function-name> [(<dummy-assignment list>) RESULT (<return-argument>)]
{<specification-statements>}
{<executable-statements>}
RETURN
END FUNCTION <function-name>
(See Computing Nth Root of X
for an example with brief explanations of the statements used)
|
Form of a Subroutine call
CALL <subroutine-name>[<actual-argument-list>]
|
(FUNCTIONS and SUBROUTINES are often grouped into the term SUBPROGRAM;
by default, arguments are passed by reference)
Specification Statements (or Type Declaration Statements)
INTEGER I,J,Row
REAL A0,B0,Max,X,Y
DOUBLE PRECISION S
LOGICAL A,B,C
COMPLEX Z,W0
CHARACTER*10 Name
IMPLICIT F,DF
EXPLICIT NONE
PARAMETER (Pi=3.1415926535)
DATA E/2.178281828/
COMMON A0,B0
COMMON /BlockA/ A,B,X,Y
(IMPLICIT and IMPLICIT NONE
are mutually exclusive. Depending on compiler settings,
INTEGER may default to INTEGER*2 or
INTEGER*4;
REAL*4 may be used for REAL;
REAL*8 may be used for DOUBLE PRECISION)
variables
Variables beginning with I,J,K,L,M,N are presumed to be integers unless
they are declared otherwise.
Assignment Statements
X = A(1) + 3
Y = A*X**2 + B*X + C
Flag = .TRUE.
Name = 'Fran'
Warning. Mixed Mode arithmetic might result in some frustrating surprises.
For example, when two integers are divided the result is an
unexpected
integer value. To avoid errors use explicit decimal points for real numbers
and the intrinsic functions REAL, INT, DBLE, and
CMPLX to get the required type conversion.
Arrays
DIMENSION X(50), Y(50)
DIMENSION A(1:50), M(1:10,1:10)
INTEGER*2 N(50)
INTEGER*2 N, X(N)
(Above can be used in Function and
Subroutine <dummy-argument-list> <specification-statements>
only.
Functions and Subroutines can use
variable bounds dimensioning for one dimensional arrays in
the passed argument list)
(see
Static and Dynamic Arrays,
Passing Pascal arrays to FORTRAN,
tir33.for Subprograms,
and D12R3MINMAX
for more information and examples on FORTRAN arrays)
Arithmetic Operations
| + |
addition |
| - |
subtraction |
| * |
multiplication |
| / |
division |
| ** |
exponentiation |
relational operators
| .EQ. |
Equal to |
| .NE. |
Not equal to |
| .LT. |
Less than |
| .GT. |
Greater than |
| .LE. |
Less than or equal to |
| .GE. |
Greater than or equal to |
|
logical operators
| .NOT. |
Complement |
| .AND. |
True if both operands are true |
| .OR. |
True if either (or both) operands are true |
| .EQV. |
True if both operands are true
OR if both operands are false
|
|
logical constants
|
Remark Statement (or Comment)
C This is a comment.
Place-holder Statement (or label)
10 CONTINUE
Unconditional Transfer
GOTO 10
Computed Control Statement
Transfers control to a specified statement, depending on the value of an
integer expression, e.g. <IJUMP>.
GOTO (100,200,300,400), IJUMP
Warning. Do not use the GOTO statement to transfer into a DO, IF, ELSE,
or ELSEIF block from outside the block.
IF (Arithmetic) Control Statement
Transfers control to a statement depending on whether the value of the
(<arithmetic-expression>) is negative, zero, or positive.
IF (<arithmetic-expression>) 100, 200, 300
IF (Logical) Control Statement
Executes a single statement only if the
(<logical-expression>) is true.
IF (<logical-expression>) GOTO 100
IF (<logical-expression>) WRITE (*,*) 'Yes'
IF (<logical-expression>) X = A+B
IF (Block) Control Statement
Performs the series of {<executable-statements>} following it or
transfers control to an ELSEIF, ELSE, or ENDIF statement, depending
on the value of the (<logical-expression>).
IF (<logical-expression>) THEN
{<executable-statements>}
ENDIF
IF (<logical-expression>) THEN
{<executable-statements>}
ELSE
{<executable-statements>}
ENDIF
IF (<logical-expression-#1>) THEN
{<executable-statements>}
ELSEIF (<logical-expression-#2>) THEN
{<executable-statements>}
ELSEIF (<logical-expression-#3>) THEN
{<executable-statements>}
ELSE
{<executable-statements>}
ENDIF
IF (ABS(P3-P1).LT.ABS(P3-P0)) THEN
U=P1
P1=P0
P0=U
V=Y1
Y1=Y0
Y0=V
ENDIF
IF (Df.EQ.0) THEN
Dp=P1-P0
P1=P0
ELSE
Dp=Y0/Df
P1=P0-Dp
ENDIF
IF (YC.EQ.0) THEN
A=C
B=C
ELSEIF ((YB*YC).GT.0) THEN
B=C
YB=YC
KR=KR+1
ELSE
A=C
YA=YC
KL=KL+1
ENDIF
DO (Block) Control Statement
DO K = M1, M2
{<executable-statements>}
ENDDO
DO K = M1, M2, Mstep
{<executable-statements>}
ENDDO
DO (M TIMES)
{<executable-statements>}
ENDDO
SUM = 0
DO K=0,100,2
SUM = SUM + K
ENDDO
SUM = 0
DO 100 K=1,100,2
SUM = SUM + K
100 CONTINUE
SUM = 0
DO K=100,-1,1
SUM = SUM + 1.0/REAL(K)
IF (SUM.GT.5) EXIT
ENDDO
DO J=1,5
DO K=1,5
A(J,K) = 1.0/FLOAT(1+J+K)
ENDDO
ENDDO
WHILE (Block) Control Statement
WHILE (<logical-expression>)
{<executable-statements>}
REPEAT
(dependent on FTN77, FTN95, etc.,
may be implemented differently)
SUM = 0
WHILE (K.LT.10000)
SUM = SUM + 1.0/REAL(K)
IF (SUM.GT.5) EXIT
K = K+1
REPEAT
Input and Output
READ *, <input-variable-name-list>
READ <format>, <input-variable-name-list>
READ <N>, <input-variable-name-list>
where <N> is a FORMAT statement number, e.g.
111 FORMAT(5X,I10,4F15.5)
READ <U,N>, <input-variable-name-list>
where <U> is a UNIT number, e.g.
READ (5,111) <input-variable-name-list>
PRINT *
PRINT *, <output-expression-list>
PRINT <format>, <output-expression-list>
PRINT '(a,F12.6)', ' ', A(I,J)
PRINT <N>, <output-expression-list>
where <N> is a FORMAT statement number, e.g.
999 FORMAT(5X,'X = ',F15.5)
WRITE (*,*)
WRITE (*,*) <output-expression-list>
WRITE (*,N) <output-expression-list>
where <N> is a FORMAT statement number, e.g.
999 FORMAT(5X,'X = ',F15.5)
WRITE (U,N) <output-expression-list>
where <U> is a UNIT number, e.g.
WRITE (6,999) <output-expression-list>
Pause Statement
PAUSE
Stop Statement
STOP
Mathematical Functions
(Check your compiler documentation; additional functions may be available;
Avoid passing expresions;
some compilers may interpret as COMPLEX number)
| COS(X) |
cosine (radians) |
| SIN(X) |
sine (radians) |
| TAN(X) |
tangent (radians) |
| COSH(X) |
Hyperbolic cosine (radians) |
| SINH(X) |
Hyperbolic sine (radians) |
| TANH(X) |
Hyperbolic tangent (radians) |
| EXP(X) |
exponential exp(x) - ex |
| ACOS(X) |
inverse cosine (-1 ⋜ X ⋜ 1) ;
-π/2 ⋜ result ⋜ π/2 radians |
| ASIN(X) |
inverse sine (-1 ⋜ X ⋜ 1) ;
-π/2 ⋜ result ⋜ π/2 radians |
| ATAN(X) |
inverse tangent (X) ;
-π/2 ⋜ result ⋜ π/2 radians |
| ATAN2(X,Y) |
inverse tangent (X,Y) ; (X, Y) are cartesian coordinates;
-π/2 ⋜ result ⋜ π/2 radians |
| ALOG(X) or LOG(X) |
natural logarithm base e |
| LOG10(X) |
common logarithm base 10 |
| SQRT(X) |
square root |
| ABS(X) |
absolute value |
| INT(X) |
conversion to integer |
| FLOAT(I) |
conversion to real number type |
| REAL(I) |
conversion to real number type |
| DBLE(X) |
conversion to double precision type |
| CMPLX(X1[, X2]) |
conversion to complex type
X1 real part, X2 imaginary part and optional
|
|