The following sections are provided:
For example: (F77 example 001)
C npac F77 example 001, showing source code layout. C C2345678--------This is a comment---------------------------------------v program myprog integer i, ifunc common /mycom/ i write(6,100) 'Hello Ken' 100 format(1x,A) i = i + 1000 call mysub i = ifunc( i ) write(6,*) i stop end subroutine mysub integer i common /mycom/ i i = i + 1 return end integer function ifunc( k ) integer k ifunc = k * k return end block data myblck common /mycom/ i data i /12345/ end C----------------------------------------------------------------------^The example code above shows how: program; subroutine; function; and block data modules are laid out in a source file.
Each program must have one and only one program module. Any number of functions and subroutines can be in the same file or in other files to be linked later.
A program is the module that is run first when the compiled and linked application is run. A subroutine is a procedure or module that can be called from a program, or from another subroutine or from a function. A function in Fortran is like a subroutine except that it returns a value. The block data module is the only permisssable way to initialise common data.
By default, Fortran 77 has an implicit typing scheme whereby a variable not explicitly declared will be automatically declared with a type determined by the first letter of its name. Specifically, by default, variables starting with the letters A,B,C,...H and O,P,Q,...Z will be REAL, variables starting with the letters I,J,K,L,M,N will be integers.
The default behaviour can be changed using the implicit statement as shown in the example.
Parameter values can be set in Fortran 77 as fixed at compile time by using the PARAMETER statement as shown in the example.
Array declaration in Fortran 77 is done either with a list of comma separated integers giving the size in each dimension of the array to be declared. The Fortran 77 standard provides for up to seven dimensional arrays, although some implementations provide more. Array indexing is by default from 1 up to the size in each dimension, although arrays can be declared as ARRAY(0:N) or even as ARRAY(-N:N), which would have N+1 and 2N+1 elements respectively.
For example: (F77 example 002)
program myprog integer maxm, maxn parameter( maxm = 20 ) parameter( maxn = 10 ) integer m, n, i, j real a(m,n), b(n), c(1:n) m = 5 n = 8 call mysub( a, b, c, m, n, maxm, maxn ) end subroutine mysub( a, b, c, m, n, maxm, maxn ) integer m, n, maxm, maxn real a(maxm,maxn), b(maxn), c(maxn) do 20 j=1,n do 10 i=1,m a(i,j) = 0.0 10 continue b(j) = 0.0 c(j) = 0.0 20 continue return end C----------------------------------------------------------------------^The example shows a (fairly useless) program which sets up two vectors and an array, and then calls a subroutine to initialize them all to zero. The sizes can be passed from the program to the subroutine at run time, which is usefil if the subroutine is part of a library and should not have sizes hard-wired into it.
The Standard format specification elements in common use are:
A Character(s) - Until run out of input or storage. An Character string exactly n characters long. Dw.d Floating D exponent format with w characters, and d decimal places. Ew.d Floating E exponent format with w characters, and d decimal places. Fw.d Floating format with w characters, d decimal places. Gw.d Floating format (tries F if possible, else E or D). nHabcd Explicit number n (4 here) of characters abcd. Iw Integer format with w characters. Lw Logical format with w characters. Tn Moves to column n with n on [1,80] nX Ignore n spaces. 'text' Explicit quotation of text. / New line (I'm not sure if this is standard).
ABS(a), IABS(a) Absolute value returning REAL, or INTEGER. AINT(a), INT(a) Integer value returning REAL or INTEGER. ANINT(a), NINT(a) Nearest Integer value returning REAL or INTEGER. REAL(a) Conversion to REAL. AIMAG(a) Imaginary part of a COMPLEX. DBLE(a) Conversion to DOUBLE PRECISION. MOD(a1,a2) Remainder from a1/a2. MAX(ai,...) Maximum value of two or more arguments. MIN(ai,...) Minimum value of two or more arguments. SIGN(a1,a2) Transfer of sign |a1|Sign(a2) returning REAL ISIGN(a1,a2) Transfer of sign |a1|Sign(a2) returning INTEGER. DIM(a1,a2) Positive difference | a1 - a2 |. EXP(a) Exponential e^a1. LOG(a) Natural Logarithm. LOG10(a) Base-10 Logarithm. SIN(a) Radian Sine. COS(a) Radian Cosine. TAN(a) Radian Tangent. ASIN(a) Radian Arcsine. ACOS(a) Radian Arccosine. ATAN(a) Radian Arctangent. ATAN2(a1,a2) Radian Arctangent of (a1/a2) SINH(a) Hyperbolic Sine COSH(a) Hyperbolic Cosine TANH(a) Hyperbolic Tangent CHAR(i) Character represented by i. ICHAR(s) INTEGER representation of character s. LEN(s) INTEGER length of string s. INDEX(s1,s2) INTEGER position of substring s2 in s1.
The following non-standard miscellaneous extra intrinsic functions are provided on many Fortran compiler implementations (specifically on DEC systems)
IAND(i1,i2) Bitwise AND. IOR(i1,i2) Bitwise OR. IEOR(i1,i2) Bitwise Exclusive-Or. NOT(i1) Bitwise inversion. ISHFT(i1, n1) Left shift of bits in i1 by n1, losing bits off the end. ( = x2) IBITS(i1, n1, n2) Extract bits n1 to n1+n2-1 of i1. IBSET(i1, n1) Set bit n1 of i1. IBCLR(i1, n1) Clear bit n1 of i1. BTEST(i1, n1) .TRUE. if bit n1 of i1. ISHFTC(i1, n1) Circular right shift of i1 by n1 (= /2) DATE(STRING) Nine character date in form: dd-mmm-yy. IDATE(I,J,K) The date: I=month (1..12) J=Day (1..31) K=Year (89..99) SECNDS(X) Returns INTEGER number of seconds since midnight minus X seconds. TIME(STRING) Eight character time in form: hh:mm:ss.