SuivantPrec.Bas prec.BasNiv. sup.

5.3 Specifying Data Type 

The preceding rules ensure that the data type of an literal constant is completely determined by its form. Similarly the data type of an expression depends on the operands and operators involved. The intrinsic functions are also a special case, since their properties, including their data types, are known to the compiler. All other typed objects in a Fortran program are referred to by symbolic names. The rules given here apply to all of these named objects: variables, arrays, named constants, statement functions, and external functions.

In many programming languages, especially those in the Algol family, the data type of almost every item in the program has to be specified explicitly. Many programmers regard it as a chore to have to provide all these type specifications, although their presence does make it rather easier for the compiler to detect mistakes.

In Fortran you can specify data types explicitly in a similar way by using type statements, but Fortran also makes life easier by having certain default types. The data type of any object which has not been declared in a type statement depends on the first letter of its name. The default rules are:
First letter of the nameImplicit type
A to H REAL
I to N INTEGER
O to Z REAL
Most programs make extensive use of integer and real objects, so these default values reduce the number of type statements that are required, provided suitable initial letters are chosen for the symbolic names.

The first-letter rule can also be changed throughout a program unit by using an IMPLICIT statement, described below.

Type Statements 

There are six different type statements, one for each data type. In their simplest form they just consist of the appropriate data-type keyword followed by a list of symbolic names. For example:
       INTEGER  AGE, GRADE
       LOGICAL  SUPER
       REAL  RATE, HOURS, PAY, TAX, INSURE
In this example the first four items declared to be real would have had that type anyway had the default rules been left to operate. Confirmatory type specification does no harm.

There is no limit to the number of type statements that can be used but a name must not have its type specified explicitly more than once in a program unit. Type statements must precede all executable statements in the unit; it is good practice, though not essential, for them to precede other specification statements referring to the same name. Type statements can be used in a subprogram to specify the types of the dummy arguments and, in an external function, the type of the function as well. Type statements by themselves have no effect on intrinsic function names but it is not a good idea to use them in this way. The CHARACTER statement is slightly different from the others because it also specifies the length of each character item, i.e. the number of characters it holds. The length can be given separately for each item, thus:       CHARACTER NAME*15, STREET*30, TOWN*20, PCODE*7 Alternatively, if several items are to have the same length, a default length for the statement can be given at the beginning:       CHARACTER*20 STAR, GALAXY, COMET*4, PLANET  This declares the name COMET to have a length of 4 characters, whereas STAR, GALAXY, and PLANET are all 20 characters long. If the length is not specified at all it defaults to one. The length can also be specified by means of a named integer constant or an integer constant expression enclosed in parentheses. For example:
        PARAMETER (NEXT=15, LAST=80)
        CHARACTER TEXT*(NEXT+LAST)
Note that the length of a character item is fixed at compilation time. The special form:       CHARACTER NAME*(*) is permitted in two cases: for named constants the length of the literal constant in the PARAMETER statement is used (section 5.4); for dummy arguments of procedures the length of the associated actual argument is used (section 9.5). Type statements can also be used to declare the dimensions of arrays: this is described in section 5.6.

IMPLICIT Statement 

The IMPLICIT statement can be used to change the first-letter default rule throughout a program unit. For example:       IMPLICIT DOUBLE PRECISION (D,X-Z), INTEGER (N-P) would mean that all names starting with the letters D,X,Y, or Z would (unless declared otherwise in type statements) have the type double precision. Similarly the letters I through P, instead of just I through N, will imply integer type. The other letters (A-C,E-H, and Q-W) will still imply real type.

IMPLICIT can be used with character type to specify a default length as well, for example:       IMPLICIT CHARACTER*100 (C,Z), CHARACTER*4 (S) But this is not usually of much practical value. As with type statements, the default character length is one.

More than one IMPLICIT statement can be used in a program unit but the same letter must not have its implied type specified more than once. The usual Fortran implied-type rules apply to all initial letters not listed in any IMPLICIT statements. The list of letters given after each type must appear in alphabetical order. IMPLICIT statements normally precede all other specification statements in a program. There is one exception to this: PARAMETER statements may precede them provided that the constants named in them are not affected by the IMPLICIT statement. Note that dummy arguments and function names may be affected by a subsequent IMPLICIT statement. IMPLICIT statements have no effect on intrinsic function names.

Guidelines 

There are two diametrically opposed schools of thought on type specification. The first holds that all names should have their types specified explicitly. This certainly helps programmers to avoid mistakes, because they have to think more carefully about each item. It also helps the compiler to diagnose errors more easily, especially if the it knows that all names are going to be declared in advance. Some Fortran compilers allow a statement of the form "IMPLICIT NONE" which makes all names typeless by default and so requiring every name to be explicitly typed. Others have a compile-time switch with the same effect. If yours does not you may be able to produce a similar effect by using something like:       IMPLICIT CHARACTER*1000000 (A-Z) near the beginning of each program unit which is likely to cause an error for anything not explicitly typed. One disadvantage of the practice of declaring all names in advance is that the program may become so cluttered with specification statements that it may obscure its structure and algorithm.

The alternative way of working is to make maximum use of implicit types to reduce the number of statements. This means, of course, that the first letter of each name has to be chosen to suit the type, leaving no more than five to be chosen freely: this makes it harder than ever to devise meaningful symbolic names. As a result, Fortran programs often include names like RIMAGE or ISIZE or KOUNT. Clearly type statements are still needed for character type because it is usually necessary to use items of a number of different lengths.

Experience suggests that either system can be satisfactory provided it is used consistently. However the wholesale reassignment of initial letters with IMPLICIT statements usually increases the chance of making a mistake. IMPLICIT, if used at all, should only reassign one or two rarely-used letters to the less common data types, for example:
       IMPLICIT DOUBLE PRECISION (Z), LOGICAL (Q),
       COMPLEX (X)
It is also prudent to use an identical IMPLICIT statement in each program unit, otherwise type mismatches are more likely to be made in procedure calls.

SuivantPrec.Bas prec.HautNiv. sup.