Table of contents:
Each procedure parameter should have its intent declared to facilitate reasoning about the flow of data in and out of the function.
By declaring the intent of each procedure parameter the flow of data in and out of the function can be understood from merely looking at the procedure declaration. 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 can use this information to enforce correct usage of procedure parameters.
Declare the proper intent for each procedure parameter.
The following code compiles correctly:
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
However, there are 2 runtime errors that could be avoided by declaring the proper intents for the subroutine parameters:
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
This code will not compile, since now the compiler has enough information to detect the errors:
- ‘a’ has INTENT(IN) and thus can not be modified.
- ‘b’ has INTENT(OUT) and thus the subroutine can not receive a value for that parameter.
You can find this and more examples at GitHub.
- PWR008 examples at GitHub
- INTENT – Intel® Fortran Compiler Developer Guide and Reference, April 2019 [last checked August 2019]
Join Parallelware Analyzer Early Access
Enter the program to have access to all versions of Parallelware Analyzer until the official release.