Issue #
Implicit data typing is enabled by default and should be avoided since it is error prone and reduces the legibility of the code.
Actions #
Add IMPLICIT NONE
after the PROGRAM or MODULE declaration statement.
Relevance #
Since Fortran 77, variables can be implicitly declared by following certain conventions. When no type is explicitly declared for a variable, the first letter of its name is used to determine its type. Thus, when the first letter is I, J, K, L, M, or N, then the data type is INTEGER; otherwise, it is REAL. This implicit behavior is discouraged since it is error prone and reduces the legibility of the code. It can be disabled by adding an “implicit none” statement.
Note
Implicit data typing may lead to unexpected runtime errors. For example, it may change the results of the program due to implicit data typing determining the type of operation used in computations. Note the example code below shows issues with integer/real division operations due to implicit data typing.
Code examples #
Fortran #
In the following example, the data type of all the variables is determined implicitly:
ROGRAM p
NUM1 = 7
NUM2 = 2.5
RES = NUM1/NUM2 ! RES = 3.0
END PROGRAM p
By disabling implicit data typing with the IMPLICIT NONE
statement, the compiler raises an error if the data types of all the variables is not declared explicitly as follows:
PROGRAM p
IMPLICIT NONE
INTEGER :: NUM1 = 7
REAL :: NUM2 = 2.5, RES
RES = NUM1/NUM2 ! RES = 2.799...
END PROGRAM p
Note that the example code above probably fixes a precision issue as well. This is due to implicit data typing of NUM2, which starts with the letter N and thus it is implicitly assigned the INTEGER type. This leads to the division NUM1/NUM2
being an integer division operation (the REAL result RES
equals 3.0
), instead of the probably intended real division operation (the REAL result RES
equals 2.799….
).
Related resources #
References #
- IMPLICIT – FORTRAN 77 Language Reference, ORACLE, 2010 [last checked May 2023]
- IMPLICIT – Intel® Fortran Compiler Developer Guide and Reference, April 2022 [last checked Jul 2023]