Each procedure parameter should have its intent declared to facilitate reasoning about the flow of data in and out of the function.
Declare the proper intent for each procedure parameter.
By declaring the intent of each procedure parameter the flow of data in and out of the function is made explicit in the source code. This not only improves code legibility but also allows reasoning about what the procedure does: by including all data inputted and outputted by a procedure in its signature, the interactions of the procedure with external data can be easily determined by inspecting the procedure call sites. Compilers and other code analysis tools take advantage of this information to enforce correctness, performance and programming best practices.
Implicit intent of variables may lead to unexpected runtime errors. For example, it may avoid the detection of errors in the code by the compiler. Note the example code below shows issues with incorrect assignment of variables and incorrect mapping of variables in the call site of the function.
Code examples #
In the following example, the intent of all the parameters of the function is not explicit in the code:
PROGRAM p IMPLICIT NONE INTEGER :: s = 2 CALL f(s, 2) CONTAINS SUBROUTINE f(a, b) IMPLICIT NONE INTEGER :: a INTEGER :: b a = 5 b = a * 2 END SUBROUTINE f END PROGRAM
By enforcing the explicit declaration of the intent of the parameters of the function, the source code looks as follows:
PROGRAM p IMPLICIT NONE INTEGER :: s = 2 CALL f(s, 2) CONTAINS SUBROUTINE f(a, b) IMPLICIT NONE INTEGER, INTENT(IN) :: a INTEGER, INTENT(OUT) :: b a = 5 b = a * 2 END SUBROUTINE f END PROGRAM
Note that the example code above raises two runtime errors due to the incorrect usage of variable’s intent. For example, a variable like
a with an
INTENT(IN) cannot be assigned inside the function. Another example is variable
b with an
INTENT(OUT) which cannot be mapped to a constant
2 at the function call site
CALL f(s, 2).
Related resources #
- INTENT – Intel® Fortran Compiler Developer Guide and Reference, April 2022 [last checked May 2023]