PURE
is required in some cases - for example, procedures called within specification expressions or from FORALL
or DO CONCURRENT
constructs. PURE
is required in these case to give the Fortran processor flexibility in the ordering of procedure invocations while still having a reasonably deterministic outcome from a particular stretch of code.
Beyond those required cases, whether to use PURE
or not is basically a question of style, which is somewhat subjective.
There are costs to use of PURE
(inability to do IO within the procedure, inability to call procedures that are not PURE
) and benefits (a pure procedure written today can be called from a context written tomorrow that requires a pure procedure, because PURE
procedures have no side effects the implications of an invocation of such a procedure may be clearer to a reader of the code), the trade-off between the two depends on specifics.
The standard may give Fortran processors considerable lee-way in how they evaluate expressions and function references within expressions. It definitely constrains programs in some ways around side effects of function execution and modification of function arguments. The requirements on a pure function are consistent with that lee-way and those constraints, consequently some people use a style where most functions are pure. Again, it may still depend on specifics, and exceptions may have to exist for things like C interoperability or interaction with external API's.
pure
routines as often as I can. It forces me to write clean code. However, only a small percentage of routines actually can be pure... But I have never seen any difference in terms of speed or such. – Dateelemental
procedures can beimpure
. – Reeferpure
when correctly coded. As I understand it from my book, only procedures which modify variables specified withintent(in)
or have I/O components would not bepure
. That seems like the minority, not majority, to me. – Selvagepure
keyword should be used whenever possible? That does not appear to be the standard method of use based on the Fortran code I've viewed. – Selvage