Statements |
4 |
![]() |
). The syntax and a description of each statement is given, along with possible restrictions and examples. A table of sample statements appears in Appendix B.
statement reads from standard input.
|
ACCEPT f [, iolist ] | |
|
ACCEPT grname | |
|
f |
Format identifier |
|
iolist |
List of variables, substrings, arrays, and records |
|
grname |
Name of the namelist group |
REAL VECTOR(10) ACCEPT *, NODE, VECTOR |
ASSIGN s TO i
s
Statement label
i
Integer variable
ASSIGN
The ASSIGN statement assigns a statement label to a variable. Syntax
Description
The label s is the label of an executable statement or a FORMAT statement.
i must be INTEGER*4 or INTEGER*8, not INTEGER*2.
While i is defined with a statement label value, do no arithmetic with i.
ASSIGN 9 TO K GO TO K ... 9 WRITE (*,*) 'Assigned ', K, ' to K' |
In the above example, the output shows the address, not 9.
INTEGER PHORMAT 2 FORMAT ( A80 ) ASSIGN 2 TO PHORMAT ... WRITE ( *, PHORMAT ) 'Assigned a FORMAT statement no.' |
v = e
e
Expression giving the value to be assigned
v
Variable, substring, array element, record, or record field
Assignment
The assignment statement assigns a value to a variable, substring, array element, record, or record field. Syntax
Description
The value can be a constant or the result of an expression. The kinds of assignment statements: are arithmetic, logical, character, and record assignments. Arithmetic Assignment
v is of numeric type and is the name of a variable, array element, or record field.
Example: An assignment statement:
Note - Compiling with any of the options -i2, -dbl, -r8, or -xtypemap can alter the default data size of variables and expressions. This is discussed in Chapter 2. See also the Sun Fortran User's Guide for a description of these options.
REAL A, B DOUBLE PRECISION V V = A * B |
The above code is compiled exactly as if it were the following:
REAL A, B DOUBLE PRECISION V V = DBLE( A * B ) |
Logical Assignment
v is the name of a variable, array element, or record field of type logical. Character Assignment
The constant can be a Hollerith constant or a string of characters delimited by apostrophes (') or quotes ("). The character string cannot include the control characters Control-A, Control-B, or Control-C; that is, you cannot hold down the Control key and press the A, B, or C keys. If you need those control characters, use the char() function. Record Assignment
v and e are each a record or record field.
Example 2: Logical assignment:
LOGICAL B1*1, B2*1 LOGICAL L3, L4 L4 = .TRUE. B1 = L4 B2 = B1 |
Example 3: Hollerith assignment:
CHARACTER S*4 INTEGER I2*2, I4*4 REAL R S = 4Hwxyz I2 = 2Hyz I4 = 4Hwxyz R = 4Hwxyz |
Example 4: Character assignment:
CHARACTER BELL*1, C2*2, C3*3, C5*5, C6*6 REAL Z C2 = 'z' C3 = 'uvwxyz' C5 = 'vwxyz' C5(1:2) = 'AB' C6 = C5 // C2 BELL = CHAR(7) ! Control Character (^G) |
C2 C3 C5 C6 |
gets 'z gets 'uvw' gets 'ABxyz' gets 'ABxyzz' an extra z left over from C5 |
BELL |
gets 07 hex Control-G, a bell |
Example 5: Record assignment and record field assignment:
AUTOMATIC vlist
vlist
List of variables and arrays
AUTOMATIC
The AUTOMATIC
statement makes each recursive invocation of the subprogram have its own copy of the specified items. It also makes the specified items become undefined outside the subprogram when the subprogram exits through a RETURN statement. Syntax
Description
For automatic variables, there is one copy for each invocation of the procedure. To avoid local variables becoming undefined between invocations, f77 classifies every variable as either static or automatic with all local variables being static by default. For other than the default, you can declare variables as static or automatic in a STATIC
, AUTOMATIC
, or IMPLICIT statement. See also the discussion of the -stackvar option in the Fortran User's Guide.
INTEGER FUNCTION NFCTRL( I ) IMPLICIT AUTOMATIC (A-Z) ... RETURN END |
Local variables and arrays are static by default, so in general, there is no need to use SAVE. You should use SAVE to ensure portability. Also, SAVE is safer if you leave a subprogram by some way other than a RETURN.
Restrictions
Automatic variables and arrays cannot appear in DATA or SAVE statements. Examples
Example: Some other uses of AUTOMATIC:
AUTOMATIC A, B, C REAL P, D, Q AUTOMATIC P, D, Q IMPLICIT AUTOMATIC (X-Z) |
Example: Structures are unpredictable if AUTOMATIC:
Restrictions
An AUTOMATIC statement and a type statement cannot be combined to make an AUTOMATIC type statement. For example, AUTOMATIC REAL X does not declare the variable X to be both AUTOMATIC and REAL; it declares the variable REALX to be AUTOMATIC.
BACKSPACE
The BACKSPACE statement positions the specified file to just before the preceding record. Syntax
Description
BACKSPACE in a terminal file has no effect.
|
Prior to Execution |
After Execution |
|
Beginning of the file |
Remains unchanged |
|
Beyond the endfile record |
Before the endfile record |
|
Beginning of the previous record |
Start of the same record |
Examples
Example 1: Simple backspace:
BACKSPACE 2 LUNIT = 2 BACKSPACE LUNIT |
Example 2: Backspace with error trap:
INTEGER CODE BACKSPACE ( 2, IOSTAT=CODE, ERR=9 ) ... 9 WRITE (*,*) 'Error during BACKSPACE' STOP |
BLOCK DATA [ name ]
name
Symbolic name of the block data subprogram in which the BLOCK DATA statement appears. This parameter is optional. It is a global name.
BLOCK DATA
The BLOCK DATA statement identifies a subprogram that initializes variables and arrays in labeled common blocks. Syntax
Description
A block data subprogram can contain as many labeled common blocks and data initializations as desired.
If an entity in a labeled common block is initially defined, all entities having storage units in the common block storage sequence must be specified, even if they are not all initially defined.
The same labeled common block cannot be specified in more than one block data subprogram in the same executable program.
The optional parameter name must not be the same as the name of an external procedure, main program, common block, or other block data subprogram in the same executable program. The name must not be the same as any local name in the subprogram.
BLOCK DATA INIT COMMON /RANGE/ X0, X1 DATA X0, X1 / 2.0, 6.0 / END |
statement specifies the type to be 1-byte integer. It optionally specifies array dimensions and initializes with values.
|
BYTE v [/c/] ... | |
|
v |
Name of a symbolic constant, variable, array, array declarator, function, or dummy function |
|
c |
List of constants for the immediately preceding name |
BYTE BIT3 / 8 /, C1 / 'W' /, & COUNTER /0/, M /127/, SWITCH / .FALSE. / |
|
CALL sub [ ( [ ar [, ar ] ... ] ) ] | |
|
sub |
Name of the subroutine to be called |
|
ar |
Actual argument to be passed to the subroutine |
The FORTRAN Standard requires that actual arguments in a CALL statement must agree in order, number, and type with the corresponding formal arguments of the referenced subroutine. The compiler checks this only when the -XlistE option is on.
Recursion is allowed. A subprogram can call itself directly, or indirectly by calling another subprogram that in turns calls this subroutine. Such recursion is nonstandard.
An actual argument, ar, must be one of the following:
Execution of the CALL statement proceeds as follows:
Note - ACALLto a subprogram defined as aFUNCTIONrather than as aSUBROUTINEwill cause unexpected results and is not recommended. The compiler does not detect such inappropriateCALLs and no warning is issued.
CHARACTER *25 TEXT TEXT = 'Some kind of text string' CALL OOPS ( TEXT ) END SUBROUTINE OOPS ( S ) CHARACTER S*(*) WRITE (*,*) S END |
Example 3: Another form of alternate return; the & is nonstandard:
CALL RANK ( N, &8, &9 ) |
Example 4: Array, array element, and variable:
REAL M(100,100), Q(2,2), Y CALL SBRX ( M, Q(1,2), Y ) ... END SUBROUTINE SBRX ( A, D, E ) REAL A(100,100), D, E ... RETURN END |
In this example, the real array M matches the real array, A, and the real array element Q(1,2) matches the real variable, D.
In the above example, the record NEW matches the record CURRENT, and the integer variable, K, matches the record field, PRIOR.OLD.
CHARACTER
The CHARACTER statement specifies the type of a symbolic constant, variable, array, function, or dummy function to be character. Syntax
Description
Each character occupies 8 bits of storage, aligned on a character boundary. Character arrays and common blocks containing character variables are packed in an array of character variables. The first character of one element follows the last character of the preceding element, without holes. Examples
Example 1: Character strings and arrays of character strings:
CHARACTER*17 A, B(3,4), V(9) CHARACTER*(6+3) C |
The above code is exactly equivalent to the following:
CHARACTER A*17, B(3,4)*17, V(9)*17 CHARACTER C*(6+3) |
Both of the above two examples are equivalent to the nonstandard variation:
CHARACTER A*17, B*17(3,4), V*17(9) ! nonstandard |
There are no null (zero-length) character-string variables. A one-byte character string assigned a null constant has the length zero.
CHARACTER S*1 S = '' |
During execution of the assignment statement, the variable S is precleared to blank, and then zero characters are moved into S, so S contains one blank; because of the declaration, the intrinsic function LEN(S) will return a length of 1. You cannot declare a size of less than 1, so this is the smallest length string variable you can get.
SUBROUTINE SWAN( A ) CHARACTER A*32 |
Example 4: Dummy argument character string with length the same as corresponding actual argument:
SUBROUTINE SWAN( A ) CHARACTER A*(*) ... |
Example 5: Symbolic constant with parenthesized asterisk:
CHARACTER *(*) INODE PARAMETER ( INODE = 'Warning: INODE corrupted!' ) |
The intrinsic function LEN(INODE) returns the actual declared length of a character string. This is mainly for use with CHAR*(*) dummy arguments.
CHARACTER A*17 A = "xyz" PRINT *, LEN( A ) END |
The above program displays 17, not 3.
CLOSE
The CLOSE statement disconnects a file from a unit. Syntax
Description
The options can be specified in any order.
Execution of a CLOSE statement specifying a unit that does not exist, or a unit that has no file connected to it, has no effect.
Execution of a CLOSE statement specifying a unit zero (standard error) is not allowed, but you can reopen it to some other file.
The unit or file disconnected by the execution of a CLOSE statement can be connected again to the same, or a different, file or unit.
Note - For tape I/O, use the TOPEN() routines.
CLOSE ( 2, STATUS='KEEP') |
CLOSE ( 2, STATUS='DELETE', IOSTAT=I ) |
Example 3: Close and keep a scratch file even though the status is SCRATCH:
OPEN ( 2, STATUS='SCRATCH') ... CLOSE ( 2, STATUS='KEEP', IOSTAT=I ) |
COMMON [/[ cb ]/] nlist [[,]/[ cb ] / nlist ] ...
cb
Common block name
nlist
List of variable names, array names, and array declarators
COMMON
The COMMON statement defines a block of main memory storage so that different program units can share the same data without using arguments. Syntax
Description
If the common block name is omitted, then blank common block is assumed. Restrictions
Formal argument names and function names cannot appear in a COMMON statement. Examples
Example 1:
DIMENSION V(100) COMMON V, M COMMON / LIMITS / I, J ... |
Unlabeled common and labeled common:
In the above example, V and M are in the unlabeled common block; I and J are defined in the named common block, LIMITS.
COMMON /X/ A COMMON /Y/ B EQUIVALENCE ( A, B) ! |
Example 3: An EQUIVALENCE statement can extend a common block on the right-hand side:
DIMENSION A(5) COMMON /X/ B EQUIVALENCE ( B, A) |
COMMON /X/ A REAL B(2) EQUIVALENCE ( A, B(2)) ! |
COMPLEX
The COMPLEX statement specifies the type of a symbolic constant, variable, array, function, or dummy function to be complex, optionally specifies array dimensions and size, and initializes with values. Syntax
Description
The declarations can be: COMPLEX, COMPLEX*8, COMPLEX*16, or COMPLEX*32. Specifying the size is nonstandard.
COMPLEX
For a declaration such as COMPLEX W, the variable W is usually two REAL*4 elements contiguous in memory, interpreted as a complex number. -dbl, -r8, or -xtypemap. See the discussion in Chapter 2 for details. COMPLEX*8
For a declaration such as COMPLEX*8 W, the variable W is always two REAL*4 elements contiguous in memory, interpreted as a complex number.
COMPLEX*16
For a declaration such as COMPLEX*16 W, W is always two REAL*8 elements contiguous in memory, interpreted as a double-width complex number.
COMPLEX*32
(SPARC, PowerPC only) For a declaration such as COMPLEX*32 W, the variable W is always two REAL*16 elements contiguous in memory, interpreted as a quadruple-width complex number.
Comments
There is a double-complex version of each complex built-in function. Generally, the specific function names begin with Z or CD instead of C, except for the two functions DIMAG and DREAL, which return a real value. Examples
Example 1: Complex variables. These statements are equivalent.
COMPLEX U, V or, COMPLEX*8 U, V or, COMPLEX U*8, V*8 |
Example 2: Initialize complex variables:
COMPLEX U / (1, 9.0) /, V / (4.0, 5 ) / |
A complex constant is a pair of numbers, either integers or reals.
COMPLEX U*16 / (1.0D0, 9 ) /, V*16 / (4.0, 5.0D0) / COMPLEX*16 X / (1.0D0, 9.0) /, Y / (4.0D0, 5 ) / |
A double-complex constant is a pair of numbers, and at least one number of the pair must be double precision.
COMPLEX U*32 / (1.0Q0, 9 ) /, V*32 / (4.0, 5.0Q0) / COMPLEX*32 X / (1.0Q0, 9.0) /, Y / (4.0Q0, 5 ) / |
A quadruple complex constant is a pair of numbers, and at least one number of the pair must be quadruple precision.
COMPLEX R*16(5), S(5)*16 COMPLEX U*32(5), V(5)*32 COMPLEX X*8(5), Y(5)*8 |
[ label ] CONTINUE
label
Executable statement number
CONTINUE
The CONTINUE statement is a "do-nothing" statement. Syntax
Description
The CONTINUE statement is often used as a place to hang a statement label, usually it is the end of a DO loop. Example
DIMENSION U(100)
S = 0.0
DO 1 J = 1, 100
S = S + U(J)
IF ( S .GE. 1000000 ) GO TO 2
1 CONTINUE
STOP
2 CONTINUE
...
DATA
The DATA statement initializes variables, substrings, arrays, and array elements. Syntax
Description
All initially defined items are defined with the specified values when an executable program begins running.
.
Taking into account the repeat factor, the number of constants in clist must be equal to the number of items in the nlist. The appearance of an array in nlist is equivalent to specifying a list of all elements in that array. Array elements can be indexed by constant subscripts only.
Note - With the -stackvar compiler option, if a variable appears on a DATA statement after an executable reference to that variable. See the Sun Fortran User's Guide.
DATA statement. Character Constants in the DATA Statement
If the length of a character item in nlist is greater than the length of the corresponding constant in clist, it is padded with blank characters on the right.
The form of an implied DO list is:
The range of an implied DO loop is dlist. The iteration count for the implied DO is computed from m1, m2, and m3, and it must be positive.
Variables
Variables can also be initialized in type statements. This is an extension of the FORTRAN Standard. Examples are given under each of the individual type statements and under the general type statement.
Examples
Example 1: Character, integer, and real scalars. Real arrays:
CHARACTER TTL*16 REAL VEC(5), PAIR(2) DATA TTL / 'Arbitrary Titles' /, & M / 9 /, N / 0 /, & PAIR(1) / 9.0 /, & VEC / 3*9.0, 0.1, 0.9 / ... |
Example 2: Arrays--implied DO:
REAL R(3,2), S(4,4) DATA ( S(I,I), I=1,4) / 4*1.0 /, & (( R(I,J), J=1,3), I=1,2) / 6*1.0 / ... |
Example 3: Mixing an integer and a character:
CHARACTER CR*1 INTEGER I*2, N*4 DATA I / '00' /, N / 4Hs12t /, CR / 13 / ... |
DECODE/ENCODE
ENCODE writes to a character variable, array, or array element.
DECODE reads from a character variable, array, or array element.
Data is edited according to the format identifier. Syntax
Description
The entities in the I/O list can be: variables, substrings, arrays, array elements, record fields. A simple unsubscripted array name specifies all of the elements of the array in memory storage order, with the leftmost subscript increasing more rapidly.
CHARACTER S*6 / '987654' /, T*6 INTEGER V(3)*4 DECODE( 6, '(3I2)', S ) V WRITE( *, '(3I3)') V ENCODE( 6, '(3I2)', T ) V(3), V(2), V(1) PRINT *, T END |
The above program has this output:
98 76 54 547698 |
The DECODE reads the characters of S as 3 integers, and stores them into V(1), V(2), and V(3).
DIMENSION a ( d ) [,a ( d )] ...
a
Name of an array
d
Specifies the dimensions of the array. It is a list of 1 to 7 declarators separated by commas.
DIMENSION
The DIMENSION statement specifies the number of dimensions for an array, including the number of elements in each dimension. Syntax
Description
This section contains descriptions for the dimension declarator and the arrays. Dimension Declarator
The lower and upper limits of each dimension are designated by a dimension declarator. The form of a dimension declarator is:
|
[ dd1 :] dd2 |
dd1 and dd2 are dimension bound expressions specifying the lower- and upper- bound values. They can be arithmetic expressions of type integer or real. They can be formed using constants, symbolic constants, formal arguments, or variables defined in the COMMON statement. Array references and references to user-defined functions cannot be used in the dimension bound expression. dd2 can also be an asterisk. If dd1 is not specified, a value of one is assumed. The value of dd1 must be less than or equal to dd2.
Adjustable Array
If the dimension declarator is an arithmetic expression that contains formal arguments or variables defined in the COMMON statement, then the array is called an adjustable array. In such cases, the dimension is equal to the initial value of the argument upon entry into the subprogram. Assumed-Size Array
The array is called an assumed-size array when the dimension declarator contains an asterisk. In such cases, the upper bound of that dimension is not stipulated. An asterisk can only appear for formal arrays and as the upper bound of the last dimension in an array declarator. Examples
Example 1: Arrays in a main program:
DIMENSION M(4,4), V(1000) ... END |
In the above example, M is specified as an array of dimensions 4 ×4 and V is specified as an array of dimension 1000.
SUBROUTINE INV( M, N ) DIMENSION M( N, N ) ... END |
In the above example, the formal arguments are an array, M, and a variable N. M is specified to be a square array of dimensions N× N.
DIMENSION HELIO (-3:3, 4, 3:9) ... END |
In the above example, HELIO is a 3-dimensional array. The first element is HELIO(-3,1,3) and the last element is HELIO(3,4,9).
SUBROUTINE ENHANCE( A, NLO, NHI ) DIMENSION A(NLO : NHI) ... END |
PARAMETER ( LO = 1, HI = 9.3 ) DIMENSION A(HI, HI*3 + LO ) ... END |
In the above example, A is an array of dimension 9×28.
SUBROUTINE ENHANCE( A, X, Y ) DIMENSION A(X : Y) ... END |
Example 7: Assumed-size arrays:
SUBROUTINE RUN(A,B,N) DIMENSION A(*), B(N,*) ... |
DO s [,] loop-control
or
DO loop-control
DO
The DO statement repeatedly executes a set of statements. Syntax
s is a statement number.
Description
The DO statement contains the following constructs. Labeled DO Loop
A labeled DO loop consists of the following:
The terminal statement should not be one of the following statements:
ELSE IF
ELSE IF
If a DO statement appears within the range of another DO loop, its range must be entirely contained within the range of the outer DO loop. More than one labeled DO loop can have the same terminal statement.
If a DO statement appears within an IF, ELSE IF, or ELSE block, the range of the associated DO loop must be contained entirely within that block.
If a block IF statement appears within the range of a DO loop, the corresponding END IF statement must also appear within the range of that DO loop.
Execution proceeds as follows:
Control must not jump into the range of a DO loop from outside its range.
If there is a jump into the range of a DO loop from outside its range, a warning is issued, but execution continues anyway.
When the jump is from outside to the terminal statement that is CONTINUE, and this statement is the terminal statement of several nested DO loops, then the most inner DO loop is always executed.
The inner loop is not executed, and at the WRITE, L is undefined. Here L is shown as 0, but that is implementation-dependent; do not rely on it.
INTEGER COUNT, OUTER COUNT = 0 DO OUTER = 1, 5 NOUT = OUTER DO INNER = 1, 3 NIN = INNER COUNT = COUNT+1 END DO END DO WRITE(*,*) OUTER, NOUT, INNER, NIN, COUNT END |
6 5 4 3 15 |
DO [ s [,]] WHILE (e)
s
Label of an executable statement
e
Logical expression
DO WHILE
The DO WHILE
statement repeatedly executes a set of statements while the specified condition is true. Syntax
Description
Execution proceeds as follows:
DO WHILE
If a DO WHILE statement appears within the range of another DO WHILE loop, its range must be entirely contained within the range of the outer DO WHILE loop. More than one DO WHILE loop can have the same terminal statement.
If a DO WHILE statement appears within an IF, ELSE IF, or ELSE block, the range of the associated DO WHILE loop must be entirely within that block.
If a block IF statement appears within the range of a DO WHILE loop, the corresponding END IF statement must also appear within the range of that DO WHILE loop.
INTEGER A(4,4), C, R ... C = 4 R = 1 DO WHILE ( C .GT. R ) A(C,R) = 1 C = C - 1 END DO |
Example 2: A DO WHILE with a statement number:
INTEGER A(4,4), C, R ... DO 10 WHILE ( C .NE. R ) A(C,R) = A(C,R) + 1 C = C+1 10 CONTINUE |
DOUBLE COMPLEX v [/c/] [, v [/c/] ...
v
Name of a symbolic constant, variable, array, array declarator, function, or dummy function
c
List of constants for the immediately preceding name
DOUBLE COMPLEX
The DOUBLE COMPLEX
statement specifies the type to be double complex. It optionally specifies array dimensions and size, and initializes with values. Syntax
Description
The declaration can be: DOUBLE COMPLEX or COMPLEX*16. DOUBLE COMPLEX
For a declaration such as DOUBLE COMPLEX Z, the variable Z is two REAL*8 elements contiguous in memory, interpreted as one double-width complex number.
DOUBLE COMPLEX Z, can be altered by compiling with any of the options -dbl, -r8, or -xtypemap. See the discussion in Chapter 2 for details. COMPLEX*16
For a declaration such as COMPLEX*16 Z, the variable Z is always two REAL*8 elements contiguous in memory, interpreted as one double-width complex number.
Comments
There is a double-complex version of each complex built-in function. Generally, the specific function names begin with Z or CD instead of C, except for the two functions, DIMAG and DREAL, which return a real value. Examples are: SIN(), CSIN(), CDSIN().
DOUBLE COMPLEX U, V DOUBLE COMPLEX W(3,6) COMPLEX*16 X, Y(5,5) COMPLEX U*16(5), V(5)*16 |
DOUBLE PRECISION v [/c/] [, v [/c/] ...
v
Name of a symbolic constant, variable, array, array declarator, function, or dummy function
c
List of constants for the immediately preceding name
DOUBLE PRECISION
The DOUBLE PRECISION statement specifies the type to be double precision, and optionally specifies array dimensions and initializes with values. Syntax
Description
The declaration can be: DOUBLE PRECISION or REAL*8. DOUBLE PRECISION
For a declaration such as DOUBLE PRECISION X, the variable X is a REAL*8 element in memory, interpreted as one double-width real number. -dbl, -r8, or -xtypemap. See the discussion in Chapter 2 for details. REAL*8
For a declaration such as REAL*8 X, the variable X is always an element of type REAL*8 in memory, interpreted as a double-width real number.
Example
For example:
DOUBLE PRECISION R, S(3,6) REAL*8 T(-1:0,5) |
IF ( e ) THEN
...
ELSE
...
END IF
e
Logical expression
ELSE
The ELSE statement indicates the beginning of an ELSE block. Syntax
Description
Execution of an ELSE statement has no effect on the program.
An ELSE block can be empty.
Restrictions
You cannot jump into an ELSE block from outside the ELSE block. Examples
Example 1: ELSE:
CHARACTER S ... IF ( S .GE. '0' .AND. S .LE. '9' ) THEN CALL PUSH ELSE CALL TOLOWER END IF ... |
Example 2: An invalid ELSE IF where an END IF is expected:
IF ( K .GT. 5 ) THEN N = 1 ELSE N = 0 ELSE IF ( K .EQ. 5 ) THEN ... |
IF ( e1 ) THEN
ELSE IF ( e2 ) THEN
END IF...
e1 and e2
Logical expressions
ELSE IF
The ELSE IF provides a multiple alternative decision structure. Syntax
Description
You can make a series of independent tests, and each test can have its own sequence of statements.
The statement label, if any, of an ELSE IF statement cannot be referenced by any statement.
A matching END IF statement of the same IF level as the ELSE IF must appear before any ELSE IF or ELSE statement at the same IF level.
READ (*,*) N IF ( N .LT. 0 ) THEN WRITE(*,*) 'N<0' ELSE IF ( N .EQ. 0) THEN WRITE(*,*) 'N=0' ELSE WRITE(*,*) 'N>0' END IF |
ENCODE/DECODE
The ENCODE
statement writes data from a list to memory. Syntax
Description
ENCODE is provided for compatibility with older versions of FORTRAN. Similar functionality can be accomplished using internal files with a formatted sequential WRITE statement. ENCODE is not in the FORTRAN Standard. Example
The DECODE reads the characters of S as 3 integers, and stores them into V(1), V(2), and V(3). The ENCODE statement writes the values V(3), V(2), and V(1), into T as characters; T then contains '547698'.
CHARACTER S*6, T*6
INTEGER V(3)*4
DATA S / '987654' /
DECODE( 6, 1, S ) V
1 FORMAT( 3 I2 )
ENCODE( 6, 1, T ) V(3), V(2), V(1)
END
The END statement indicates the end of a program unit. Syntax
END
Description
The END statement:
In the FORTRAN Standard, the END statement cannot be continued, but f77 allows this practice.
No other statement, such as an END IF statement, can have an initial line that appears to be an END statement.
PROGRAM MAIN WRITE( *, * ) 'Very little' END |
END DO
The END DO statement terminates a DO loop.
Syntax
END DO
Description
The END DO statement is the delimiting statement of a Block DO statement. If the statement label is not specified in a DO statement, the corresponding terminating statement must be an END DO statement. You can branch to an END DO statement only from within the range of the DO loop that it terminates. Examples
Example 1: A DO loop with a statement number:
DO 10 N = 1, 100 ... 10 END DO |
Example 2: A DO loop without statement number:
DO N = 1, 100 ... END DO |
END FILE
The END FILE statement writes an end-of-file record as the next record of the file connected to the specified unit. Syntax
Description
If you are using the ENDFILE statement and other standard FORTRAN I/O for tapes, we recommend that you use the TOPEN() routines instead, because they are more reliable. Restrictions
u must be connected for sequential access. Execution of an END FILE statement on a direct-access file is not defined in the FORTRAN Standard, and is unpredictable. Do not use an END FILE statement on a direct-access file. Examples
Example 1: Constants:
END FILE 2 END FILE ( 2 ) END FILE ( UNIT=2 ) |
LOGUNIT = 2 END FILE LOGUNIT END FILE ( LOGUNIT ) END FILE ( UNIT=LOGUNIT ) |
NOUT = 2 END FILE ( UNIT=NOUT, IOSTAT=KODE, ERR=9) ... 9 WRITE(*,*) 'Error at END FILE, on unit', NOUT STOP |
END IF
END IF
The END IF statement ends the block IF that the IF began. Syntax
Description
For each block IF statement there must be a corresponding END IF statement in the same program unit. An END IF statement matches if it is at the same IF level as the block IF statement. Examples
Example 1: IF/END IF:
IF ( N .GT. 0 )THEN N = N+1 END IF |
IF ( N .EQ. 0 ) THEN N = N+1 ELSE N = N-1 END IF |
END MAP
The END MAP
statement terminates the MAP declaration. Syntax
END MAP
Description
See Section , "UNION and MAP."
Restrictions
The MAP statement must be within a UNION statement. Example
...
MAP
CHARACTER *16 MAJOR
END MAP
...
END STRUCTURE
The END STRUCTURE
statement terminates the STRUCTURE statement. Syntax
END STRUCTURE
Description
See Section , "STRUCTURE."
Example
STRUCTURE /PROD/
INTEGER*4 ID
CHARACTER*16 NAME
CHARACTER*8 MODEL
REAL*4 COST
REAL*4 PRICE
END STRUCTURE
END UNION
The END UNION
statement terminates the UNION statement. Syntax
END UNION
Description
See Section , "UNION and MAP."
Example
UNION
MAP
CHARACTER*16
END MAP
MAP
INTEGER*2 CREDITS
CHARACTER *8 GRAD_DATE
END MAP
END UNION
ENTRY
The ENTRY statement defines an alternate entry point within a subprogram. Syntax
Description
Note these nuances for the ENTRY statement: Procedure References by Entry Names
An ENTRY name used in a subroutine subprogram is treated like a subroutine and can be referenced with a CALL statement. Similarly, the ENTRY name used in a function subprogram is treated like a function and can be referenced as a function reference. Parameter Correspondence
The formal arguments of an ENTRY statement need not be the same in order, number, type, and name as those for FUNCTION, SUBROUTINE, and other ENTRY statements in the same subprogram. Each reference to a function, subroutine, or entry must use an actual argument list that agrees in order, number, type, and name with the dummy argument list in the corresponding FUNCTION, SUBROUTINE, or ENTRY statement.
ENTRY statements that specify alternate return arguments can be used only in subroutine subprograms, not functions. Restrictions
An ENTRY statement cannot be used within a block IF construct or a DO loop. Examples
Example 1: Multiple entry points in a subroutine:
SUBROUTINE FIN( A, B, C ) INTEGER A, B CHARACTER C*4 ... RETURN ENTRY HLEP( A, B, C ) ... RETURN ENTRY MOOZ ... RETURN END |
In the above example, the subroutine FIN has two alternate entries: the entry HLEP has an argument list; the entry MOOZ has no argument list.
INTEGER A, B CHARACTER C*4 ... CALL FIN( A, B, C ) ... CALL MOOZ ... CALL HLEP( A, B, C ) ... |
In the above example, the order of the call statements need not match the order of the entry statements.
REAL FUNCTION F2 ( X ) F2 = 2.0 * X RETURN ENTRY F3 ( X ) F3 = 3.0 * X RETURN ENTRY FHALF ( X ) FHALF = X / 2.0 RETURN END |
EQUIVALENCE ( nlist ) [, ( nlist ) ] ...
nlist
List of variable names, array element names, array names, and character substring names separated by commas
EQUIVALENCE
The EQUIVALENCE statement specifies that two or more variables or arrays in a program unit share the same memory. Syntax
Description
An EQUIVALENCE statement stipulates that the storage sequence of the entities whose names appear in the list nlist must have the same first memory location. Restrictions
In nlist, dummy arguments and functions are not permitted.
DIMENSION A (2) EQUIVALENCE (A(1),B), (A(2),B) |
REAL A (2) DOUBLE PRECISION D (2) EQUIVALENCE (A(1), D(1)), (A(2), D(2)) |
When COMMON statements and EQUIVALENCE statements are used together, several additional rules can apply. For such rules, refer to the notes on the COMMON statement.
Example
The association of A, B, and C can be graphically illustrated as follows:
CHARACTER A*4, B*4, C(2)*3
EQUIVALENCE (A,C(1)),(B,C(2))
|
|
01 |
02 |
03 |
04 |
05 |
06 |
07 |
A |
A(1) |
A(2) |
A(3) |
A(4) |
|
|
|
B |
|
|
|
B(1) |
B(2) |
B(3) |
B(4) |
C |
|
C(1) |
|
|
C(2) |
|
|
EXTERNAL proc [, proc ] ...
proc
Name of external procedure, dummy procedure, or block data routine.
EXTERNAL
The EXTERNAL statement specifies procedures or dummy procedures as external, and allows their symbolic names to be used as actual arguments. Syntax
Description
If an external procedure or a dummy procedure is an actual argument, it must be in an EXTERNAL statement in the same program unit. Restrictions
A subroutine or function name can appear in only one of the EXTERNAL statements of a program unit. Examples
Example 1: Use your own version of TAN:
EXTERNAL TAN T = TAN( 45.0 ) ... END FUNCTION TAN( X ) ... RETURN END |
Example 2: Pass a user-defined function name as an argument:
REAL AREA, LOW, HIGH EXTERNAL FCN ... CALL RUNGE ( FCN, LOW, HIGH, AREA ) ... END FUNCTION FCN( X ) ... RETURN END SUBROUTINE RUNGE ( F, X0, X1, A ) ... RETURN END |
label FORMAT ( f )
label
Statement number
f
Format specification list
FORMAT
The FORMAT statement specifies the layout of the input or output records. Syntax
The items in f have the form:
|
[ r ] d | |
|
[ r ] ( f ) | |
|
r |
A repeat factor |
|
d |
An edit descriptor (repeatable or nonrepeatable). If r is present, then d must be repeatable. |
The repeatable edit descriptors are:
|
I Iw Iw.m O Ow Ow.m Z Zw Zw.m |
F Fw Fw.m A Aw L Lw |
E Ew Ew.m Ew.m.e Ew.mEe |
D Dw Dw.m Dw.m.e Dw.mEe |
G Gw Gw.m Gw.m.e Gw.mEe |
'a1a2 ... an'
[k]R
k defaults to 10
"a1a2 ... an"
[k]P
k defaults to 0
nHa1a2 ... an
S
$
SU
/
SP
:
SS
B
Tn
BN
nT
BZ
TL[n]
n defaults to 1
TR[n]
n defaults to 1
[n]X
n defaults to 1
Nonrepeatable Edit Descriptors
Variable Format Expressions
In general, any integer constant in a format can be replaced by an arbitrary expression enclosed in angle brackets:
|
1 FORMAT( ... < e > ... ) |
The n in an nH... edit descriptor cannot be a variable format expression.
Description
The FORMAT statement includes the explicit editing directives to produce or use the layout of the record. It is used with formatted input/output statements and ENCODE/DECODE statements. Repeat Factor
r must be a nonzero, unsigned, integer constant. Repeatable Edit Descriptors
The descriptors I, O, Z, F, E, D, G, L, and A indicate the manner of editing and are repeatable. Nonrepeatable Edit Descriptors
The descriptors are the following:
In some sense, the comma can be omitted anywhere the meaning is clear without it, but, other than those cases listed above, this is nonstandard. u
For formats in variables, invalid format strings cause warnings or error messages at runtime.
For variable format expressions, of the form <e>, invalid format strings cause warnings or error messages at compile time or runtime.
See Chapter 5, "Input and Output," for more details and more examples.
Examples
Example 1: Some A, I, and F formats:
READ( 2, 1 ) PART, ID, HEIGHT, WEIGHT 1 FORMAT( A8, 2X, I4, F8.2, F8.2 ) WRITE( 9, 2 ) PART, ID, HEIGHT, WEIGHT 2 FORMAT( 'Part:', A8, ' Id:', I4, ' Height:', F8.2, & ' Weight:', F8.2 ) |
Example 2: Variable format expressions:
DO 100 N = 1, 50 ... 1 FORMAT( 2X, F<N+1>.2 ) |
[ type ] FUNCTION fun ( [ ar [, ar ] ... ] )
FUNCTION (External)
The FUNCTION statement identifies a program unit as a function subprogram. Syntax
type is one of the following:
(
COMPLEX*32 and REAL*16 are SPARC, PowerPC only.)
|
[ type ] FUNCTION name [* m]([ ar [,ar] ...]) | |
|
m |
Unsigned, nonzero integer constant specifying length of the data type. |
Description
Note the type, value, and formal arguments for a FUNCTION statement. Type of Function
The function statement involves type, name, and formal arguments.
Note - Compiling with any of the options -dbl, -r8, -i2, or -xtypemap can alter the default data size assumed in the call to or definition of functions unless the data type size is explicitly declared. See Chapter 2 and the Fortran User Guide for details on these options.
Value of Function
The symbolic name of the function must appear as a variable name in the subprogram. The value of this variable, at the time of execution of the RETURN or END statement in the function subprogram, is the value of the function. Formal Arguments
The list of arguments defines the number of formal arguments. The type of these formal arguments is defined by some combination of default, type statements, IMPLICIT statements, and DIMENSION statements. Restrictions
Alternate return specifiers are not allowed in FUNCTION statements. Examples
Example 1: Character function:
CHARACTER*5 FUNCTION BOOL(ARG) BOOL = 'TRUE' IF (ARG .LE. 0) BOOL = 'FALSE' RETURN END |
In the above example, BOOL is defined as a function of type CHARACTER with a length of 5 characters. This function when called returns the string, TRUE or FALSE, depending on the value of the variable, ARG.
FUNCTION SQR (A) SQR = A*A RETURN END |
In the above example, the function SQR is defined as function of type REAL by default, and returns the square of the number passed to it.
INTEGER FUNCTION FCN*2 ( A, B, C ) |
The above nonstandard form is treated as:
INTEGER*2 FUNCTION FCN ( A, B, C ) |
GO TO i [ [,] ( s [, s ] ... ) ]
i
Integer variable name
s
Statement label of an executable statement
GO TO (Assigned)
The assigned GO TO statement branches to a statement label identified by the assigned label value of a variable. Syntax
Description
Execution proceeds as follows:
i must be INTEGER*4 or INTEGER*8, not INTEGER*2.
s must be in the same program unit as the GO TO statement.
The same statement label can appear more than once in a GO TO statement.
The statement control jumps to must be executable, not DATA, ENTRY, FORMAT, or INCLUDE.
Control cannot jump into a DO, IF, ELSE IF, or ELSE block from outside the block.
ASSIGN 10 TO N ... GO TO N ( 10, 20, 30, 40 ) ... 10 CONTINUE ... 40 STOP |
GO TO ( s [, s ] ... ) [,] e
s
Statement label of an executable statement
e
Expression of type integer or real
GO TO (Computed)
The computed GO TO statement selects one statement label from a list, depending on the value of an integer or real expression, and transfers control to the selected one. Syntax
Description
Execution proceeds as follows:
The same statement label can appear more than once in a GO TO statement.
The statement control jumps to must be executable, not DATA, ENTRY, FORMAT, or INCLUDE.
Control cannot jump into a DO, IF, ELSE IF, or ELSE block from outside the block.
... GO TO ( 10, 20, 30, 40 ), N ... 10 CONTINUE ... 20 CONTINUE ... 40 CONTINUE |
:
|
GO TO s | |
|
s |
Statement label of an executable statement |
The statement control jumps to must be executable, not a DATA, ENTRY, FORMAT, or INCLUDE statement.
Control cannot jump into a DO, IF, ELSE IF, or ELSE block from outside the block.
A = 100.0 B = 0.01 GO TO 90 ... 90 CONTINUE |
|
IF ( e ) s1, s2, s3 | |
e |
Arithmetic expression: integer, real, double precision, or quadruple precision |
s1, s2, s3 |
Labels of executable statements |
The restrictions are:
N = 0 IF ( N ) 10, 20, 30 |
|
IF ( e ) THEN | |
|
... |
|
|
END IF | |
|
e |
A logical expression |
Example: In the following program, the IF-level of statement 9 is 2-1, or, 1:
IF ( X .LT. 0.0 ) THEN MIN = NODE END IF ... 9 IF ( Y .LT. 0.0 ) THEN MIN = NODE - 1 END IF |
The IF-level of every statement must be zero or positive. The IF-level of each block IF, ELSE IF, ELSE, and END IF statement must be positive. The IF-level of the END statement of each program unit must be zero.
IF Block
An IF block consists of all the executable statements following the block IF statement, up to, but not including, the next ELSE, ELSE IF, or END IF statement that has the same if level as the block IF statement. An IF block can be empty. In the following example, the two assignment statements form an IF block:
IF ( X .LT. Y ) THEN M = 0 N = N+1 END IF |
Execution proceeds as follows:
IF ( L ) THEN N=N+1 CALL CALC ELSE K=K+1 CALL DISP END IF |
Example 2: IF-THEN-ELSE-IF with ELSE-IF:
IF ( C .EQ. 'a' ) THEN NA=NA+1 CALL APPEND ELSE IF ( C .EQ. 'b' ) THEN NB=NB+1 CALL BEFORE ELSE IF ( C .EQ. 'c' ) THEN NC=NC+1 CALL CENTER END IF |
Example 3: Nested IF-THEN-ELSE:
IF ( PRESSURE .GT 1000.0 ) THEN IF ( N .LT. 0.0 ) THEN X = 0.0 Y = 0.0 ELSE Z = 0.0 END IF ELSE IF ( TEMPERATURE .GT. 547.0 ) THEN Z = 1.0 ELSE X = 1.0 Y = 1.0 END IF |
IF ( e ) st
e
Logical expression
st
Executable statement
IMPLICIT type ( a [, a ] ... ) [, type ( a [, a ] ... ) ]
or:
IMPLICIT NONE
or:
IMPLICIT UNDEFINED(A-Z) u
IF (Logical)
The logical IF statement executes one single statement, or does not execute it, depending on the value of a logical expression. Syntax
Description
The logical IF statement evaluates a logical expression and executes the specified statement if the value of the logical expression is true. The specified statement is not executed if the value of the logical expression is false, and execution continues as though a CONTINUE statement had been executed. Example
IF ( VALUE .LE. ATAD ) CALL PUNT ! Note that there is no THEN.
IF ( TALLY .GE. 1000 ) RETURN
IMPLICIT
The IMPLICIT statement confirms or changes the default type of names. Syntax
type is one of the following permitted types:
Description
The different uses for implicit typing and no implicit typing are described here. Implicit Typing
The IMPLICIT statement can also indicate that no implicit typing rules apply in a program unit.
Note - Compiling with any of the options -dbl, -i2, -r8, or -xtypemap can alter the assumed size of names typed with an IMPLICIT statement that does not specify a size: IMPLICIT REAL (A-Z). See Chapter 2 and the Fortran User's Guide for details.
No Implicit Typing
The second form of IMPLICIT specifies that no implicit typing should be done for user-defined names, and all user-defined names shall have their types declared explicitly. Restrictions
IMPLICIT statements must precede all other specification statements.
Examples
Example 1: IMPLICIT: everything is integer:
IMPLICIT INTEGER (A-Z) X = 3 K = 1 STRING = 0 |
Example 2: Complex if it starts with U, V, or W; character if it starts with C or S:
IMPLICIT COMPLEX (U,V,W), CHARACTER*4 (C,S) U1 = ( 1.0, 3.0) STRING = 'abcd' I = 0 X = 0.0 |
Example 3: All items must be declared:
IMPLICIT NONE CHARACTER STR*8 INTEGER N REAL Y N = 100 Y = 1.0E5 STR = 'Length' |
In the above example, once IMPLICIT NONE is specified in the beginning. All the variables must be declared explicitly.
IMPLICIT INTEGER (A-Z) IMPLICIT REAL (A-C) C = 1.5E8 D = 9 |
In the above example, D through Z implies INTEGER, and A through C implies REAL.
INCLUDE 'file'
or:
INCLUDE "file"
file
Name of the file to be inserted
INCLUDE
The INCLUDE
statement inserts a file into the source program. Syntax
Description
The contents of the named file replace the INCLUDE statement. Search Path
If the name referred to by the INCLUDE statement begins with the character /, then it is taken by f77 to mean the absolute path name of the INCLUDE file. Otherwise, f77 looks for the file in the following directories, in this order:
These INCLUDE statements can be nested ten deep.
-vax=spec compiler options are set.
|
"lname1=path1; lname2=path2; ... " |
INCLUDE 'stuff' |
The above line is replaced by the contents of the file stuff.
INCLUDE 'ver1/const.h' |
For a standard install, f77 searches these directories:
|
INQUIRE( [ UNIT=] u, slist ) |
An inquire by file has the general form:
|
INQUIRE( FILE=fn, slist ) | |
|
fn |
Name of the file being queried |
|
u |
Unit of the file being queried |
|
slist |
Specifier list |
The INQUIRE slist can include one or more of the following, in any order:
Inquire either by unit or by file, but not by both in the same statement.
In this system environment, the only way to discover what permissions you have for a file is to use the ACCESS(3F) function. The INQUIRE statement does not determine permissions.
The specifiers for INQUIRE are:
OPEN( 1, FILE='/dev/console' ) |
The following table summarizes the INQUIRE options:
* indicates non-standard for inquire-by-unit, but accepted by f77.
indicates non-standard for inquire-by-file, but accepted by f77.
LOGICAL OK INQUIRE( UNIT=3, OPENED=OK ) IF ( OK ) CALL GETSTD ( 3, STDS ) |
LOGICAL THERE INQUIRE( FILE='.profile', EXIST=THERE ) IF ( THERE ) CALL GETPROFILE( FC, PROFILE ) |
Example 3: More than one answer, omitting the UNIT=:
CHARACTER FN*32 LOGICAL HASNAME, OK INQUIRE ( 3, OPENED=OK, NAMED=HASNAME, NAME=FN ) IF ( OK .AND. HASNAME ) PRINT *, 'Filename="', FN, '"' |
INTEGER
The INTEGER statement specifies the type to be integer for a symbolic constant, variable, array, function, or dummy function. Syntax
Description
The declarations can be: INTEGER, INTEGER*2, INTEGER*4, INTEGER*8.
For a declaration such as INTEGER
INTEGER H, the variable H is usually one INTEGER*4 element in memory, interpreted as a single integer number. Specifying the size is nonstandard.
-dbl, -i2, -r8, or -xtypemap. See the discussion in Chapter 2 for details.
For a declaration such as INTEGER*2
INTEGER*2 H, the variable H is always an INTEGER*2 element in memory, interpreted as a single integer number.
For a declaration such as INTEGER*4
INTEGER*4 H, the variable H is always an INTEGER*4 element in memory, interpreted as a single integer number.
For a declaration such as INTEGER*8
INTEGER*8 H, the variable H is always an INTEGER*8 element in memory, interpreted as a single integer number. Restrictions
Do not use INTEGER*8 variables or 8-byte constants or expressions when indexing arrays, otherwise, only 4 low-order bytes are taken into account. This action can cause unpredictable results in your program if the index value exceeds the range for 4-byte integers. Examples
Example 1: Each of these integer declarations are equivalent:
INTEGER U, V(9) or INTEGER*4 U, V(9) or INTEGER U*4, V(9)*4 |
INTEGER U / 1 /, V / 4 /, W*2 / 1 /, X*2 / 4 / |
INTRINSIC fun [, fun ] ...
fun
Function name
INTRINSIC
The INTRINSIC statement lists intrinsic functions that can be passed as actual arguments. Syntax
Description
If the name of an intrinsic function is used as an actual argument, it must appear in an INTRINSIC statement in the same program unit.
INTRINSIC SIN, COS X = CALC ( SIN, COS ) |
Restrictions
A symbolic name must not appear in both an EXTERNAL and an INTRINSIC statement in the same program unit.
LOGICAL
The LOGICAL statement specifies the type to be logical for a symbolic constant, variable, array, function, or dummy function. Syntax
Description
The declarations can be: LOGICAL, LOGICAL*1, LOGICAL*2, LOGICAL*4, LOGICAL*8.
For a declaration such as LOGICAL
LOGICAL H, the variable H is usually one INTEGER*4 element in memory, interpreted as a single logical value. Specifying the size is nonstandard.
-dbl, -i2,-r8, or -xtypemap. See the discussion in Chapter 2 for details.
For a declaration such as LOGICAL*1
LOGICAL*1 H, the variable H is always an BYTE element in memory, interpreted as a single logical value.
For a declaration such as LOGICAL*2
LOGICAL*2 H, the variable H is always an INTEGER*2 element in memory, interpreted as a single logical value.
For a declaration such as LOGICAL*4
LOGICAL*4 H, the variable H is always an INTEGER*4 element in memory, interpreted as a single logical value.
For a declaration such as LOGICAL*8
LOGICAL*8 H, the variable H is always an INTEGER*8 element in memory, interpreted as a single logical value. Examples
Example 1: Each of these declarations are equivalent:
LOGICAL U, V(9) or LOGICAL*4 U, V(9) or LOGICAL U*4, V(9)*4 |
LOGICAL U /.false./, V /0/, W*4 /.true./, X*4 /'z'/ |
MAP
field-declaration
...
[field-declaration]
END MAP
MAP
The MAP
declaration defines alternate groups of fields in a union. Syntax
Description
Each field declaration can be one of the following:
STRUCTURE /STUDENT/ CHARACTER*32 NAME INTEGER*2 CLASS UNION MAP CHARACTER*16 MAJOR END MAP MAP INTEGER*2 CREDITS CHARACTER*8 GRAD_DATE END MAP END UNION END STRUCTURE |
NAMELIST / grname / namelist [[,] / grname / namelist ] ...
grname
Symbolic name of the group
namelist
List of variables and arrays
NAMELIST
The NAMELIST
statement defines a list of variables or array names, and associates it with a unique group name. Syntax
Description
The NAMELIST statement contains a group name and other items. Group Name
The group name is used in the namelist-directed I/O statement to identify the list of variables or arrays that are to be read or written. This name is used by namelist-directed I/O statements instead of an input/output list. The group name must be unique, and identifies a list whose items can be read or written. Namelist Items
The namelist items can be of any data type. The items in the namelist can be variables or arrays, and can appear in more than one namelist. Only the items specified in the namelist can be read or written in namelist-directed I/O, but it is not necessary to specify data in the input record for every item of the namelist. Restrictions
Input data can assign values to the elements of arrays or to substrings of strings that appear in a namelist.
Example
Example: The NAMELIST statement:
CHARACTER*16 SAMPLE LOGICAL*4 NEW REAL*4 DELTA NAMELIST /CASE/ SAMPLE, NEW, DELTA |
In this example, the group CASE has three variables: SAMPLE, NEW, and DELTA.
OPEN( KEYWORD1=value1, KEYWORD2=value2, ... )
KEYWORDn
A valid keyword specifier, as listed below
OPEN
The OPEN statement connects an existing external file to a unit, or creates a file and connects it to a unit, or changes some specifiers of the connection.
Note - For tape I/O, use the TOPEN() routines.
Syntax
Description
The OPEN statement determines the type of file named, whether the connection specified is legal for the file type (for instance, DIRECT access is illegal for tape and tty devices), and allocates buffers for the connection if the file is on tape or if the subparameter FILEOPT='BUFFER=n' is specified. Existing files are never truncated on opening. The options can be specified in any order.
Details of the OPEN keyword specifier are listed in the following table.
Examples
Here are six examples.
OPEN( UNIT=8, FILE='projectA/data.test' ) OPEN( 8, FILE='projectA/data.test' ) |
In the above example, these properties are established by default: sequential access, formatted file, and (unwisely) no allowance for error during file open.
OPEN( UNIT=8, FILE='projectA/data.test', & ACCESS='SEQUENTIAL', FORM='FORMATTED' ) |
Example 3: Either of these opens file, fort.8, and connects it to unit 8:
OPEN( UNIT=8 ) OPEN( 8 ) |
In the above example, you get sequential access, formatted file, and no allowance for error during file open. If the file, fort.8 does not exist before execution, it is created. The file remains after termination.
OPEN( UNIT=8, FILE='projectA/data.test', ERR=99 ) |
The above statement branches to 99 if an error occurs during OPEN.
OPEN( 1, ACCESS='DIRECT', recl=1 ) |
For more information on variable-length records, see "Direct Access I/O" on page 265.
OPEN( 1, STATUS='SCRATCH' ) |
This statement opens a temporary file with a name, such as tmp.FAAAa003zU. The file is usually in the current working directory, or in TMPDIR if that environment variable is set.
OPTIONS /qualifier [/qualifier ...]
OPTIONS
The OPTIONS
statement overrides compiler command-line options. Syntax
Description
The following table shows the OPTIONS statement qualifiers:
Restrictions
The OPTIONS statement must be the first statement in a program unit; it must be before the BLOCK DATA, FUNCTION, PROGRAM, and SUBROUTINE statements. Example
For the following source, integer variables declared with no explicit size occupy 4 bytes rather than 2, with or without the -i2 option on the command line. This rule does not change the size of integer constants, only variables.
OPTIONS /I4 PROGRAM FFT ... END |
By way of contrast, if you use /NOI4, then all integer variables declared with no explicit size occupy 2 bytes rather than 4, with or without the -i2 option on the command line. However, integer constants occupy 2 bytes with -i2, and 4 bytes otherwise.
PARAMETER ( p=e [, p=e ] ... )
p
Symbolic name
e
Constant expression
PARAMETER
The PARAMETER statement assigns a symbolic name to a constant. Syntax
An alternate syntax is allowed, if the -xl flag is set:
|
PARAMETER p=e [, p=e ] ... |
In this alternate form, the type of the constant expression determines the type of the name; no conversion is done.
Description
e can be of any type and the type of symbolic name and the corresponding expression must match.
No structured records or record fields are allowed in a constant expression.
Restrictions
A symbolic constant must not be defined more than once in a program unit. Examples
Example 1: Some real, character, and logical parameters:
CHARACTER HEADING*10 LOGICAL T PARAMETER ( EPSILON=1.0E-6, PI=3.141593, & HEADING='IO Error #', & T=.TRUE. ) ... |
Example 2: Let the compiler count the characters:
CHARACTER HEADING*(*) PARAMETER ( HEADING='I/O Error Number' ) ... |
Example 3: The alternate syntax, if the -xl compilation flag is specified:
PARAMETER FLAG1 = .TRUE. |
The above statement is treated as:
LOGICAL FLAG1 PARAMETER (FLAG1 = .TRUE.) |
An ambiguous statement that could be interpreted as either a PARAMETER statement or an assignment statement is always taken to be the former, as long as either the -xl or -xld option is specified.
PARAMETER S = .TRUE. |
With -xl, the above statement is a PARAMETER statement about the variable S.
PARAMETER S = .TRUE. |
It is not an assignment statement about the variable PARAMETERS.
PARAMETERS = .TRUE. |
PAUSE [str ]
str
String of not more than 5 digits or a character constant
PAUSE
The PAUSE statement suspends execution, and waits for you to type: go. Syntax
Description
The PAUSE statement suspends program execution temporarily, and waits for acknowledgment. On acknowledgment, execution continues.
PAUSE. To resume execution, type: go Any other input will terminate the program. |
After you type: go, execution continues as if a CONTINUE statement is executed. See this example:
If stdin is not a tty I/O device, PAUSE displays a message like this:
PAUSE: To resume execution, type: kill -15 pid |
where pid is the process ID.
demo% a.out < mydatafile PAUSE: To resume execution, type: kill -15 20537 demo% |
demo% kill -15 20537 |
POINTER ( p1, v1 ) [, ( p2, v2 ) ... ]
v1, v2
Pointer-based variables
p1, p2
Corresponding pointers
POINTER
The POINTER
statement establishes pairs of variables and pointers. Syntax
Description
Each pointer contains the address of its paired variable. Usage
Normal use of pointer-based variables involves the following steps. The first two steps can be in either order.
There are three procedures used to manage memory with pointers:
Compare:
A pointer-based variable cannot itself be a pointer.
The pointer-based variables can be of any type, including structures.
No storage is allocated when such a pointer-based variable is defined, even if there is a size specification in the type statement.
You cannot use a pointer-based variable as a dummy argument or in COMMON, EQUIVALENCE, DATA, or NAMELIST statements.
The dimension expressions for pointer-based variables must be constant expressions in main programs. In subroutines and functions, the same rules apply for pointer-based array variables as for dummy arguments--the expression can contain dummy arguments and variables in common. Any variables in the expressions must be defined with an integer value at the time the subroutine or function is called.
This implementation of POINTER follows more along the line of Cray, not Fortran 90, although it does not follow Cray exactly.
The address cannot exceed the range of INTEGER*4. If the address expression is not in the range (-2147483648, 2147483647), then the results are unpredictable.
If you use an optimization level greater than -O2, you must write your programs with the following restrictions on the use of pointers:
COMMON A, B, C POINTER ( P, V ) P = LOC(A) + 4 ! ... |
The compiler assumes that a reference through P can change A, but not B; this assumption could produce incorrect code.
Examples
Example 1: A simple POINTER statement:
POINTER ( P, V ) |
Here, V is a pointer-based variable, and P is its associated pointer.
* ptr1.f: Assign an address via LOC() POINTER ( P, V ) CHARACTER A*12, V*12 DATA A / 'ABCDEFGHIJKL' / P = LOC( A ) PRINT *, V(5:5) END |
In the above example, the CHARACTER statement allocates 12 bytes of storage for A, but no storage for V; it merely specifies the type of V because V is a pointer-based variable. You then assign the address of A to P, so now any use of V refers to A by the pointer P. The program prints an E.
POINTER ( P1, X ), ( P2, Y ), ( P3, Z ) ... P1 = MALLOC ( 36 ) ... CALL FREE ( P1 ) ... |
:
In the above example, you get 36 bytes of memory from MALLOC() and then, after some other instructions, probably using that chunk of memory, tell FREE() to return those same 36 bytes to the memory manager.
POINTER ( P, V ) CHARACTER V*12, Z*1 P = MALLOC( 12 ) ... END |
:
In the above example, you obtain 12 bytes of memory from the function MALLOC() and assign the address of that block of memory to the pointer P.
This is a slightly more realistic example. The size might well be some large number, say, 10,000. Once that's allocated, the subroutines perform their tasks, not knowing that the array was dynamically allocated.
demo% f77 -silent Linked.f "Linked.f", line 6: Warning: local variable "b" never used "Linked.f", line 31: Warning: local variable "b" never used demo% a.out 1 aaa 2 bbb 3 ccc 4 ddd demo% |
|
PRINT f [, iolist ] | |
|
PRINT grname | |
|
f |
Format identifier |
|
iolist |
List of variables, substrings, arrays, records, ... |
|
grname |
Name of the namelist group |
Execution proceeds as follows:
Recursive I/O does not work reliably. If you list a function in an I/O list, and if that function does I/O, then during runtime, the execution may freeze, or some other unpredictable problem may occur. This risk exists independent of parallelization.
Example: Recursive I/O fails intermittently:
PRINT *, x, f(x) ! Not allowed, f() does I/O. END FUNCTION F(X) PRINT *, X RETURN END |
Examples
Example 1: Formatted scalars:
CHARACTER TEXT*16 PRINT 1, NODE, TEXT 1 FORMAT ( I2, A16 ) |
Example 2: List-directed array:
PRINT *, I, J, ( VECTOR(I), I = 1, 5 ) |
INTEGER VECTOR(10) PRINT '( 12 I2 )', I, J, VECTOR |
CHARACTER LABEL*16 REAL QUANTITY INTEGER NODE NAMELIST /SUMMARY/ LABEL, QUANTITY, NODE PRINT SUMMARY |
PROGRAM pgm
pgm
Symbolic name of the main program
PROGRAM
The PROGRAM statement identifies the program unit as a main program. Syntax
Description
For the loader, the main program is always named MAIN. The PROGRAM statement serves only the person who reads the program. Restrictions
The PROGRAM statement can appear only as the first statement of the main program.
The FORTRAN Standard does not allow this practice.
PROGRAM US_ECONOMY NVARS = 2 NEQS = 2 ... |
READ( [ UNIT=] u [, [ FMT=] f ] [, IOSTAT= ios ] [, REC= rn ] [, END= s ] [, ERR= s ] ) iolist
READ grname
READ
The READ statement reads data from a file or the keyboard to items in the list.
Note - For tape, it is more reliable to use the TOPEN() routines.
Syntax
An alternate to the UNIT=u, REC=rn form is as follows:
READ f [, iolist ]
READ([UNIT=] u, [NML=] grname [,IOSTAT=ios ] [,END=s ] [,ERR=s ] )
|
READ( u 'rn ... ) iolist |
The options can be specified in any order.
Description
The READ statement accepts the following arguments. Unit Identifier
u is either an external unit identifier or an internal file identifier.
Unformatted data transfer from internal files and terminal files is not allowed, hence, f must be present for such files.
List-directed data transfer from direct-access and internal files is allowed; hence, f can be an asterisk for such files.
If a file is connected for formatted I/O, unformatted data transfer is not allowed, and vice versa.
The END=s and REC=rn specifiers can be present in the same READ statement.
|
READ f [, iolist ] |
|
READ ( [ NML= ] grname ) |
The above two forms operate the same way as the others, except that reading from the keyboard is implied.
|
OPEN( u, FILE='FORT.u', STATUS='OLD', | ||
& |
ACCESS='SEQUENTIAL', FORM=fmt ) | |
READ( 1, 2, ERR=8, END=9, IOSTAT=N ) X, Y ... 8 WRITE( *, * ) 'I/O error # ', N, ', on 1' STOP 9 WRITE( *, * ) 'EoF on 1' RETURN END |
Example 2: Direct, unformatted read, trap I/O errors, and I/O status:
READ( 1, REC=3, IOSTAT=N, ERR=8 ) V ... 4 CONTINUE RETURN 8 WRITE( *, * ) 'I/O error # ', N, ', on 1' END |
Example 3: List-directed read from keyboard:
READ( *, * ) A, V or READ *, A, V |
Example 4: Formatted read from an internal file:
CHARACTER CA*16 / 'abcdefghijklmnop' /, L*8, R*8 READ( CA, 1 ) L, R 1 FORMAT( 2 A8 ) |
Example 5: Read an entire array:
DIMENSION V(5) READ( 3, '(5F4.1)') V |
Example 6: Namelist-directed read:
CHARACTER SAMPLE*16 LOGICAL NEW*4 REAL DELTA*4 NAMELIST /G/ SAMPLE, NEW, DELTA ... READ( 1, G ) or READ( UNIT=1, NML=G ) or READ( 1, NML=G ) |
REAL
The REAL statement specifies the type of a symbolic constant, variable, array, function, or dummy function to be real, and optionally specifies array dimensions and size, and initializes with values. Syntax
Description
Following are descriptions for REAL, REAL*4, REAL*8, and REAL*16. REAL
For a declaration such as REAL W, the variable W is usually a REAL*4 element in memory, interpreted as a real number. Specifying the size is nonstandard.
-dbl, -r8, or -xtypemap. See the discussion in Chapter 2 for details. REAL*4
For a declaration such as REAL*4 W, the variable W is always a REAL*4 element in memory, interpreted as a single-width real number.
REAL*8
For a declaration such as REAL*8 W, the variable W is always a REAL*8 element in memory, interpreted as a double-width real number.
REAL*16
(SPARC, PowerPC only) For a declaration such as REAL*16 W, the variable W is always an element of type REAL*16 in memory, interpreted as a quadruple-width real.
Examples
Example 1: Simple real variables--these declarations are equivalent:
REAL U, V(9) or REAL*4 U, V(9) or REAL U*4, V(9)*4 |
Example 2: Initialize variables (REAL*16 is SPARC, PowerPC only):
REAL U/ 1.0 /, V/ 4.3 /, D*8/ 1.0 /, Q*16/ 4.5 / |
Example 3: Specify dimensions for some real arrays:
REAL A(10,100), V(10) REAL X*4(10), Y(10)*4 |
Example 4: Initialize some arrays:
REAL A(10,100) / 1000 * 0.0 /, B(2,2) / 1.0, 2.0, 3.0, 4.0 / |
Example 5: Double and quadruple precision (REAL*16 is SPARC, PowerPC only):
REAL*8 R REAL*16 Q DOUBLE PRECISION D |
In the above example, D and R are both double precision; Q is quadruple precision.
RECORD /struct-name/ record-list [,/struct-name/ record-list]...
struct-name
Name of a previously declared structure
record-list
List of variables, arrays, or array declarators
RECORD
The RECORD
statement defines variables to have a specified structure, or arrays to be arrays of variables with such structures. Syntax
Description
A structure is a template for a record. The name of the structure is included in the STRUCTURE statement, and once a structure is thus defined and named, it can be used in a RECORD statement. Restrictions
STRUCTURE /PRODUCT/ INTEGER*4 ID CHARACTER*16 NAME CHARACTER*8 MODEL REAL*4 COST REAL*4 PRICE END STRUCTURE RECORD /PRODUCT/ CURRENT, PRIOR, NEXT, LINE(10) ... |
Each of the three variables CURRENT, PRIOR, and NEXT is a record which has the PRODUCT structure, and LINE is an array of 10 such records.
The above program produces the following output:
82 CacheBoard 1000.00 96K |
RETURN [ e ]
e
Expression of type INTEGER or REAL
RETURN
A RETURN statement returns control to the calling program unit. Syntax
Description
Execution of a RETURN statement terminates the reference of a function or subroutine.
in the SUBROUTINE statement. Examples
Example 1: Standard return:
CHARACTER*25 TEXT TEXT = "Some kind of minor catastrophe" ... CALL OOPS ( TEXT ) STOP END SUBROUTINE OOPS ( S ) CHARACTER S* 32 WRITE (*,*) S RETURN END |
REWIND
REWIND positions the file associated with the specified unit to its initial point. Syntax
Description
The options can be specified in any order. Examples
Example 1: Simple form of unit specifier:
ENDFILE 3 REWIND 3 READ (3,'(I2)') I REWIND 3 READ (3,'(I2)')I |
Example 2: REWIND with the UNIT=u form of unit specifier and error trap:
INTEGER CODE ... REWIND (UNIT = 3) REWIND (UNIT = 3, IOSTAT = CODE, ERR = 100) ... 100 WRITE (*,*) 'error in rewinding' STOP |
SAVE [ v [, v ] ... ]
v
Name of an array, variable, or common block (enclosed in slashes), occurring in a subprogram
SAVE
The SAVE statement preserves items in a subprogram after the RETURN or END statements are executed, preventing them from becoming undefined. Syntax
Description
SAVE variables are placed in an internal static area. All common blocks are already preserved because they have been allocated to a static area. Therefore, common block names specified in SAVE statements are allowed but ignored. Restrictions
The following constructs must not appear in a SAVE statement:
SUBROUTINE FFA(N) DIMENSION A(1000,1000), V(1000) SAVE A ... RETURN END |
fun ( [ d [, d ] ... ] ) = e
fun
Name of statement function being defined
d
Statement function dummy argument
e
Expression. e can be any of the types arithmetic, logical, or character.
Statement Function
A statement function statement is a function-like declaration, made in a single statement. Syntax
Description
If a statement function is referenced, the defined calculations are inserted.
ROOT( A, B, C ) = (-B + SQRT(B**2-4.0*A*C))/(2.0*A) |
The statement function argument list indicates the order, number, and type of arguments for the statement function.
PARAMETER ( PI=3.14159 ) REAL RADIUS, VOLUME SPHERE ( R ) = 4.0 * PI * (R**3) / 3.0 READ *, RADIUS VOLUME = SPHERE( RADIUS ) ... |
Example 2: Logical statement function:
LOGICAL OKFILE INTEGER STATUS OKFILE ( I ) = I .LT. 1 READ( *, *, IOSTAT=STATUS ) X, Y IF ( OK FILE(STATUS) ) CALL CALC ( X, Y, A ) ... |
Example 3: Character statement function:
CHARACTER FIRST*1, STR*16, S*1 FIRST(S) = S(1:1) READ( *, * ) STR IF ( FIRST(STR) .LT. " " ) CALL CONTROL ( S, A ) ... |
STATIC list
list
List of variables and arrays
STATIC
The STATIC
statement ensures that the specified items are stored in static memory. Syntax
Description
All local variables and arrays are classifed static by default: there is exactly one copy of each datum, and its value is retained between calls. You can also explicitly define variables as static or automatic in a STATIC or AUTOMATIC statement, or in any type statement or IMPLICIT statement.
STATIC REAL X does not declare the variable X to be both STATIC and REAL; it declares the variable REALX to be STATIC.
STATIC A, B, C REAL P, D, Q STATIC P, D, Q IMPLICIT STATIC (X-Z) |
|
STOP [ [ str ] | |
|
str |
String of no more that 5 digits or a character constant |
If str is not specified, no message is displayed.
stop 9 |
STOP: 9 |
stop 'error' |
STOP: error |
STRUCTURE
The STRUCTURE
statement organizes data into structures. Syntax
Each field declaration can be one of the following:
A structure is a template for a record. The name of the structure is included in the STRUCTURE statement, and once a structure is thus defined and named, it can be used in a RECORD statement.
The record is a generalization of the variable or array--where a variable or array has a type, the record has a structure. Where all the elements of an array must be of the same type, the fields of a record can be of different types.
If slashes are present, a name must be present.
You can specify the field-list within nested structures only.
There must be at least one field-declaration.
Each structure-name must be unique among structures, although you can use structure names for fields in other structures or as variable names.
The only statements allowed between the STRUCTURE statement and the END STRUCTURE statement are field-declaration statements and PARAMETER statements. A PARAMETER statement inside a structure declaration block is equivalent to one outside.
You can initialize a field that is a variable, array, substring, substructure, or union.
STRUCTURE /PRODUCT/ INTEGER*4 ID / 99 / CHARACTER*16 NAME CHARACTER*8 MODEL / 'Z' / REAL*4 COST REAL*4 PRICE END STRUCTURE RECORD /PRODUCT/ CURRENT, PRIOR, NEXT, LINE(10) |
In the above example, a structure named PRODUCT is defined to consist of the fields ID, NAME, MODEL, COST, and PRICE. Each of the three variables, CURRENT, PRIOR, and NEXT, is a record which has the PRODUCT structure, and LINE is an array of 10 such records. Every such record has its ID initially set to 99, and its MODEL initially set to Z.
STRUCTURE /VARLENSTR/ INTEGER*4 NBYTES CHARACTER A*25 END STRUCTURE RECORD /VARLENSTR/ VLS VLS.NBYTES = 0 |
The above structure matches the one used by the Sun Pascal compiler, pc, for varying length strings. The 25 is arbitrary.
SUBROUTINE sub [ ( [ fd [, fd ] ... ])]
sub
Name of subroutine subprogram
d
Variable name, array name, record name, or dummy procedure name, an asterisk, or an ampersand
SUBROUTINE
The SUBROUTINE statement identifies a named program unit as a subroutine, and specifies arguments for it. Syntax
Description
A subroutine subprogram must have a SUBROUTINE statement as the first statement. A subroutine can have any other statements, except a BLOCK DATA, FUNCTION, PROGRAM, or another SUBROUTINE statement.
SUBROUTINE SHR ( A, B ) CHARACTER A*8 REAL B(10,10) ... RETURN END |
Example 2: Standard alternate returns:
TYPE f [, iolist ]
or:
TYPE grname
f
Format identifier
iolist
List of output variables
grname
Name of the namelist group
TYPE
The TYPE
statement writes to stdout. Syntax
Description
The TYPE statement is provided for compatibility and is equivalent to:
INTEGER V(5) REAL X(9), Y NAMELIST /GNAM/ X, Y .... TYPE 1, V 1 FORMAT( 5 I3 ) .... TYPE GNAM .... |
The Type Statement
The type statement specifies the data type of items in the list, optionally specifies array dimensions, and initializes with values. Syntax
type can be preceded by either AUTOMATIC or STATIC.
n, as in CHARACTER*n, must be greater than 0.
COMPLEX*32 and REAL*16 are SPARC and PowerPC only. Description
A type statement can be used to:
The general form of a type statement is:
|
type VariableName / constant / ... |
|
or: |
|
type ArrayName / constant, ... / |
|
or: |
|
type ArrayName / r*constant / |
|
where r is a repeat factor. |
Example: Various type statements:
CHARACTER LABEL*12 / 'Standard' / COMPLEX STRESSPT / ( 0.0, 1.0 ) / INTEGER COUNT / 99 /, Z / 1 / REAL PRICE / 0.0 /, COST / 0.0 / REAL LIST(8) / 0.0, 6*1.0, 0.0 / |
When you initialize a data type, remember the following restrictions:
INTEGER Z / 4 / POINTER ( x, Z ) |
Note - Compiling with any of the options-dbl,-r8,-i2, or-xtypemapcan alter the default size of names typed without an explicit size. See the discussion in Chapter 2.
A type statement must precede all executable statements.
INTEGER*2 I, J/0/ REAL*4 PI/3.141592654/,ARRAY(10)/5*0.0,5*1.0/ CHARACTER*10 NAME CHARACTER*10 TITLE/'Heading'/ |
statement defines groups of fields that share memory at runtime.
UNION map-declaration map-declaration [map-declaration] ... END UNION |
The syntax of a UNION declaration is as follows:
The syntax of a MAP declaration is:
|
MAP field-declaration [field-declaration] ... [field-declaration] END MAP |
Description
A MAP statement defines alternate groups of fields in a union. During execution, one map at a time is associated with a shared storage location. When you reference a field in a map, the fields in any previous map become undefined, and are succeeded by the fields in the map of the newly referenced field. Also:
Each field-declaration in a map declaration can be one of the following:
STRUCTURE /STUDENT/ CHARACTER*32 NAME INTEGER*2 CLASS UNION MAP CHARACTER*16 MAJOR END MAP MAP INTEGER*2 CREDITS CHARACTER*8 GRAD_DATE END MAP END UNION END STRUCTURE RECORD /STUDENT/ PERSON |
In the above example, the variable PERSON has the structure /STUDENT/, so:
statement is treated the same as the DIMENSION statement.
|
VIRTUAL a ( d ) [, a ( d ) ] ... | |
|
a |
Name of an array |
|
a(d) |
Specifies the dimension of the array. It is a list of 1 to 7 declarators separated by commas |
VIRTUAL M(4,4), V(1000) ... END |
statement prevents optimization on the specified items.
|
VOLATILE nlist | |
|
nlist |
List of variables, arrays, or common blocks |
PROGRAM FFT INTEGER NODE*2, NSTEPS*2 REAL DELTA, MAT(10,10), V(1000), X, Z COMMON /INI/ NODE, DELTA, V ... VOLATILE V, Z, MAT, /INI/ ... EQUIVALENCE ( X, V ) ... |
In the above example, the array V, the variable Z, and the common block /INI/ are explicitly specified as VOLATILE. The variable X is VOLATILE through an equivalence.
WRITE
The WRITE statement writes data from the list to a file.
Note - For tape I/O, use the TOPEN() routines.
Syntax
The options can be specified in any order.
|
WRITE( u ' rn ... ) iolist |
See Example 3, later on in this section.
Description
Unit Identifier
u is either an external unit identifier or an internal file identifier.
f must not be an asterisk for direct access.
f can be an asterisk for internal files.
If a file is connected for formatted I/O, unformatted data transfer is prohibited, and vice versa.
If the output item is a character expression that employs the concatenation operator, the length specifiers of its operands can be an asterisk (*). This rule is nonstandard.
If a function appears in the output list, that function must not cause an input/output statement to be executed.
WRITE(*,*) x, f(x) ! Not allowed, f() does I/O.
END
FUNCTION F(X)
WRITE(*,*) X
RETURN
END
OPEN( u, FILE='FORT.u', STATUS='UNKNOWN', & ACCESS='SEQUENTIAL', FORM=fmt ) |
The value of fmt is 'FORMATTED' if the write is formatted, and 'UNFORMATTED' otherwise.
Examples
Example 1: Formatted write with trap I/O errors and I/O status:
WRITE( 1, 2, ERR=8, IOSTAT=N ) X, Y RETURN ... 8 WRITE( *, * ) 'I/O error # ', N, ', on 1' STOP END |
Example 2: Direct, unformatted write, trap I/O errors, and I/O status:
... WRITE( 1, REC=3, IOSTAT=N, ERR=8 ) V ... 4 CONTINUE RETURN 8 WRITE( *, * ) 'I/O error # ', N, ', on 1' END |
Example 3: Direct, alternate syntax (equivalent to above example):
... WRITE( 1 ' 3, IOSTAT=N, ERR=8 ) V ... 4 CONTINUE RETURN 8 WRITE( *, * ) 'I/O error # ', N, ', on 1' END |
Example 4: List-directed write to screen:
WRITE( *, * ) A, V or PRINT *, A, V |
Example 5: Formatted write to an internal file:
CHARACTER CA*16, L*8 /'abcdefgh'/, R*8 /'ijklmnop'/ WRITE( CA, 1 ) L, R 1 FORMAT( 2 A8 ) |
Example 6: Write an entire array:
DIMENSION V(5) WRITE( 3, '(5F4.1)') V |
Example 7: Namelist-directed write:.
CHARACTER SAMPLE*16 LOGICAL NEW*4 REAL DELTA*4 NAMELIST /G/ SAMPLE, NEW, DELTA ... WRITE( 1, G ) or WRITE( UNIT=1, NML=G ) or WRITE( 1, NML=G ) |