I'm currently delving into Fortran and I've come across the pure
keyword specifying functions/subroutines that have no side effects.
I have a book, Fortran 90/95 by S Chapman which introduces the pure
keyword but strangely provides no "good coding practice" uses.
I'm wondering how liberally one should use this keyword in their procedures. Just by looking around it's obvious to me that most procedures without side effects don't find it necessary to include the pure
keyword.
So where is it best used? Only in procedures one wants to completely guarantee have no side effects? Or perhaps in procedures one plans to convert to elemental
procedures later? (As elemental
procedures must first be pure
.)
As suggested by chw21, the primary motivation of
PURE
is to allow the compiler to better optimize. In particular, the lack ofPURE
for a function will prevent parallelization due to unknown side effects. Note thatPURE
subroutines, unlike functions, may haveINTENT(INOUT)
arguments, but there is still the restriction on side effects (and that aPURE
procedure can call only otherPURE
procedures.)Up through Fortran 2003,
ELEMENTAL
procedures are implicitlyPURE
. Fortran 2008 adds anIMPURE
prefix that can be used withELEMENTAL
procedures to disable that aspect.PURE
is required in some cases - for example, procedures called within specification expressions or fromFORALL
orDO 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 notPURE
) and benefits (a pure procedure written today can be called from a context written tomorrow that requires a pure procedure, becausePURE
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.
If you try to change a variable that has
INTENT(IN)
, it won't compile.Pure functions can only have
INTENT(IN)
arguments, and returns a value that depends only on the arguments. A pure subroutine can modifyINTENT(OUT)
andINTENT(INOUT)
arguments, but again, only based on the value of the arguments.In both cases: Same Arguments -> same result.
The advantage is that it guarantees to the compiler that it can swap the order of execution around (in order to optimise the code) without changing the behaviour of the program.