This part completes what we have learned so far about specification statements.
The implicit typing rules of Fortran 77 still hold. However, it is good practice to explicitly type all variables, and this can be forced by inserting the statement
IMPLICIT NONEat the beginning of each prorgam unit.
A named constant can be specified directly by adding the PARAMETER attribute and the constant values to a type statement:
REAL, DIMENSION(3), PARAMETER :: field = (/ 0., 1., 2. /) TYPE(triplet), PARAMETER :: t = triplet( 0., (/ 0., 0., 0. /) )
The DATA statement can be used also for arrays and variables of derived type. It is also the only way to initialise just parts of such objects, as well as to initialise to binary , octal or hexadecimal values:
TYPE(triplet) :: t1, t2 DATA t1/triplet( 0., (/ 0., 1., 2. /) )/, t2%u/0./ ! only one component of t2 initialized DATA array(1:64) / 64*0/ ! only a section of array initialized DATA i, j, k/ B'01010101', O'77', Z'ff'/
There are many variations on the way character arrays may be specified. Among the shortest and longest are
CHARACTER name(4, 5)*20 CHARACTER (KIND = kanji, LEN = 20), DIMENSION (4, 5) :: name
The values used in DATA and PARAMETER statements, or in specification statements with these attributes, are constant expressions that may include references to: array and structure constructors, elemental intrinsic functions with integer or character arguments and results, and the six transformational functions REPEAT, SELECTED_INT_KIND, TRIM, SELECTED_REAL_KIND, RESHAPE and TRANSFER:
INTEGER, PARAMETER :: long = SELECTED_REAL_KIND(12), array(3) = (/ 1, 2, 3 /)
It is possible to specify details of variables using any non-constant, scalar, integer expression that may also include inquiry function references:
SUBROUTINE s(b, m, c) USE mod ! contains a REAL, DIMENSION(:, :) :: b ! assumed-shape array REAL, DIMENSION(UBOUND(b, 1) + 5) :: x ! automatic array INTEGER m CHARACTER(LEN=*) c ! assumed-length CHARACTER(LEN= m + LEN(c)) cc ! automatic object REAL (SELECTED_REAL_KIND(2*PRECISION(a))) z ! precision of z twice that of a
These attributes are used in specifications in modules to limit the scope of entities. The attribute form is
REAL, PUBLIC :: x, y, z ! default INTEGER, PRIVATE :: u, v, w
and the statement form is
PUBLIC :: x, y, z, OPERATOR(.add.) PRIVATE :: u, v, w, ASSIGNMENT(=), OPERATOR(*)
The statement form has to be used to limit access to operators, and can also be used to change the overall default:
PRIVATE ! sets default for module PUBLIC :: only_this
For a derived data type there are three possibilities: the type and its components are all PUBLIC, the type is PUBLIC and its components PRIVATE (the type only is visible and one can change its details easily), or all of it is PRIVATE (for internal use in the module only):
MODULE mine PRIVATE TYPE, PUBLIC :: list REAL x, y TYPE(list), POINTER :: next END TYPE list TYPE(list) :: tree : END MODULE mine
To gain access to entities in a module, we use the USE statement. It has options to resolve name clashes if an imported name is the same as a local one:
USE mine, local_list => listor to restrict the used entities to a specified set:
USE mine, ONLY : list
These may be combined:
USE mine, ONLY : local_list => list