Implicit data typing is enabled by default and should be avoided since it is error prone and reduces the legibility of the code.
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: 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.
Add ‘IMPLICIT NONE’ after the PROGRAM or MODULE declaration statement.
In the following example, one might expect RES to be approximately 2.8:
PROGRAM p NUM1 = 7 NUM2 = 2.5 RES = NUM1/NUM2 ! RES = 3.0 END PROGRAM p
However, due to implicit data typing since both variable names start with N they are implicitly assigned INTEGER type and an integer division takes place. When implicit data typing is disabled through ‘IMPLICIT NONE’ the variable types must be explicit in their declaration, leading to the fix of this error:
PROGRAM p IMPLICIT NONE INTEGER :: NUM1 = 7 REAL :: NUM2 = 2.5, RES RES = NUM1/NUM2 ! RES = 2.799... END PROGRAM p
You can find this and more examples at GitHub.
- PWR007 examples at GitHub
- IMPLICIT – FORTRAN 77 Language Reference, ORACLE, 2010 [last checked September 2022]
- IMPLICIT – Intel® Fortran Compiler Developer Guide and Reference, April 2022 [last checked September 2022]
Building performance into the code from day one with Codee