User:CYRIS JUNIX FORTRAN ADFLICTI/sandbox
![]() | Warning This page contains syntax errors ("cite%20note") caused by a VisualEditor bug. Do not copy/move content from this page until the errors have been repaired. See {{Warning VisualEditor bug}} for more information. |
CYRIS JUNIX FORTRAN, your pin is 758641. Please confirm your email address
Fortran 95 language features
[edit]From Wikipedia, the free encyclopedia Jump to navigation Jump to search
"Fortran language features" redirects here. For features of previous versions of the language, see Fortran § History.
This is an overview of Fortran 95 language features. Included are the additional features of TR-15581:Enhanced Data Type Facilities, that have been universally implemented. Old features that have been superseded by new ones are not described — few of those historic features are used in modern programs although most have been retained in the language to maintain backward compatibility. Although the current standard is Fortran 2008, even many of those features first introduced into Fortran 2003 are still being implemented.[1] The additional features of Fortran 2003 and Fortran 2008 are described by Metcalf, Reid and Cohen.[2][3]
Contents
[edit]- 1 Language elements
- 2 Expressions and assignments
- 3 Control statements
- 4 Program units and procedures
- 5 Array handling
- 6 Pointers
- 7 Intrinsic procedures
- 8 Data transfer
- 9 Operations on external files
- 10 References
Language elements
[edit]Fortran is case-insensitive. The convention of writing Fortran keywords in upper case and all other names in lower case is adopted in this article; except, by way of contrast, in the input/output descriptions (Data transfer and Operations on external files).
Basics
[edit]The basic component of the Fortran language is its character set. Its members are
- the letters A ... Z and a ... z (which are equivalent outside a character context)
- the numerals 0 ... 9
- the underscore _
- the special characters
= : + blank - * / ( ) [ ] , . $ ' ! " % & ; < > ?
Tokens that have a syntactic meaning to the compiler are built from those components. There are six classes of tokens:
Label | 123
|
---|---|
Constant | 123.456789_long
|
Keyword | ALLOCATABLE
|
Operator | .add.
|
Name | solve_equation (up to 31 characters, including _)
|
Separator | / ( ) (/ /) [ ] , = => : :: ; %
|
From the tokens, statements are built. These can be coded using the new free source form which does not require positioning in a rigid column structure:
FUNCTION string_concat(s1, s2) ! This is a comment TYPE (string), INTENT(IN) :: s1, s2 TYPE (string) string_concat string_concat%string_data = s1%string_data(1:s1%length) // & s2%string_data(1:s2%length) ! This is a continuation string_concat%length = s1%length + s2%length END FUNCTION string_concat
Note the trailing comments and the trailing continuation mark. There may be 39 continuation lines, and 132 characters per line. Blanks are significant. Where a token or character constant is split across two lines:
... start_of& &_name ... 'a very long & &string'
a leading &
on the continued line is also required.
Automatic conversion of source form for existing programs can be carried out by [[1]].
Its options are
- significant blank handling;
- indentation;
- CONTINUE replaced by END DO;
- name added to subprogram END statement; and
- INTEGER*2 etc. syntax converted.
Intrinsic data types
[edit]Fortran has five intrinsic data types: INTEGER
, REAL
, COMPLEX
, LOGICAL
and CHARACTER
. Each of those types can be additionally characterized by a kind. Kind, basically, defines internal representation of the type: for the three numeric types, it defines the precision and range, and for the other two, the specifics of storage representation. Thus, it is an abstract concept which models the limits of data types' representation; it is expressed as a member of a set of whole numbers (e.g. it may be {1, 2, 4, 8} for integers, denoting bytes of storage), but those values are not specified by the Standard and not portable. For every type, there is a default kind, which is used if no kind is explicitly specified. For each intrinsic type, there is a corresponding form of literal constant. The numeric types INTEGER
and REAL
can only be signed (there is no concept of sign for type COMPLEX
).
Literal constants and kinds
[edit]INTEGER
[edit]Integer literal constants of the default kind take the form
1 0 -999 32767 +10
Kind can be defined as a named constant. If the desired range is ±10kind, the portable syntax for defining the appropriate kind, two_bytes
is
INTEGER, PARAMETER :: two_bytes = SELECTED_INT_KIND(4)
that allows subsequent definition of constants of the form
-1234_two_bytes +1_two_bytes
Here, two_bytes
is the kind type parameter; it can also be an explicit default integer literal constant, like
-1234_2
but such use is non-portable.
The KIND function supplies the value of a kind type parameter:
KIND(1) KIND(1_two_bytes)
and the RANGE
function supplies the actual decimal range (so the user must make the actual mapping to bytes):
RANGE(1_two_bytes)
Also, in DATA
(initialization) statements, binary (B), octal (O) and hexadecimal (Z) constants may be used (often informally referred to as "BOZ constants"):
B'01010101' O'01234567' Z'10fa'
REAL
[edit]There are at least two real kinds—the default and one with greater precision (this replaces DOUBLE PRECISION
). SELECTED_REAL_KIND
functions returns the kind number for desired range and precision; for at least 9 decimal digits of precision and a range of 10−99 to 1099, it can be specified as:
INTEGER, PARAMETER :: long = SELECTED_REAL_KIND(9, 99)
and literals subsequently specified as
1.7_long
Also, there are the intrinsic functions
KIND(1.7_long) PRECISION(1.7_long) RANGE(1.7_long)
that give in turn the kind type value, the actual precision (here at least 9), and the actual range (here at least 99).
COMPLEX
[edit]COMPLEX
data type is built of two integer or real components:
(1, 3.7_long)
LOGICAL
[edit]There are only two basic values of logical constants: .TRUE.
and .FALSE.
. Here, there may also be different kinds. Logicals don't have their own kind inquiry functions, but use the kinds specified for INTEGER
s; default kind of LOGICAL
is the same as of INTEGER.
.FALSE. .true._one_byte
and the KIND
function operates as expected:
KIND(.TRUE.)
CHARACTER
[edit]The forms of literal constants for CHARACTER
data type are
'A string' "Another" 'A "quote"' '''''''
(the last being an empty string). Different kinds are allowed (for example, to distinguish ASCII and UNICODE strings), but not widely supported by compilers. Again, the kind value is given by the KIND
function:
KIND('ASCII')
Number model and intrinsic functions
[edit]The numeric types are based on number models with associated inquiry functions (whose values are independent of the values of their arguments; arguments are used only to provide kind). These functions are important for portable numerical software:
DIGITS(X)
|
Number of significant digits |
EPSILON(X)
|
Almost negligible compared to one (real) |
HUGE(X)
|
Largest number |
MAXEXPONENT(X)
|
Maximum model exponent (real) |
MINEXPONENT(X)
|
Minimum model exponent (real) |
PRECISION(X)
|
Decimal precision (real, complex) |
RADIX(X)
|
Base of the model |
RANGE(X)
|
Decimal exponent range |
TINY(X)
|
Smallest positive number (real) |
Scalar variables
[edit]Scalar variables corresponding to the five intrinsic types are specified as follows:
INTEGER(KIND=2) :: i REAL(KIND=long) :: a COMPLEX :: current LOGICAL :: Pravda CHARACTER(LEN=20) :: word CHARACTER(LEN=2, KIND=Kanji) :: kanji_word
where the optional KIND
parameter specifies a non-default kind, and the ::
notation delimits the type and attributes from variable name(s) and their optional initial values, allowing full variable specification and initialization to be typed in one statement (in previous standards, attributes and initializers had to be declared in several statements). While it is not required in above examples (as there are no additional attributes and initialization), most Fortran-90 programmers acquire the habit to use it everywhere.
LEN=
specifier is applicable only to CHARACTER
s and specifies the string length (replacing the older *len
form). The explicit KIND=
and LEN=
specifiers are optional:
CHARACTER(2, Kanji) :: kanji_word
works just as well.
There are some other interesting character features. Just as a substring as in
CHARACTER(80) :: line ... = line(i:i) ! substring
was previously possible, so now is the substring
'0123456789'(i:i)
Also, zero-length strings are allowed:
line(i:i-1) ! zero-length string
Finally, there is a set of intrinsic character functions, examples being
ACHAR
|
IACHAR (for ASCII set)
|
ADJUSTL
|
ADJUSTR
|
LEN_TRIM
|
INDEX(s1, s2, BACK=.TRUE.)
|
REPEAT
|
SCAN (for one of a set)
|
TRIM
|
VERIFY (for all of a set)
|
Derived data types
[edit]For derived data types, the form of the type must be defined first:
TYPE person CHARACTER(10) name REAL age END TYPE person
and then, variables of that type can be defined:
TYPE(person) you, me
To select components of a derived type, %
qualifier is used:
you%age
Literal constants of derived types have the form TypeName(1stComponentLiteral, 2ndComponentLiteral, ...)
:
you = person('Smith', 23.5)