Intel® Fortran Compiler 17.0 Developer Guide and Reference

PURE

Keyword: Asserts that a user-defined procedure has no side effects.

Description

This kind of procedure is specified by using the prefix PURE or the prefix ELEMENTAL without the prefix IMPURE in a FUNCTION or SUBROUTINE statement.

A pure procedure has no side effects. It has no effect on the state of the program, except for the following:

The following intrinsic and library procedures are implicitly pure:

A statement function is pure only if all functions that it references are pure.

Except for procedure arguments and pointer arguments, the following intent must be specified in the specification part of the procedure for all dummy arguments:

A local variable declared in a pure procedure (including variables declared in any internal procedure) must not:

The following variables have restricted use in pure procedures (and any internal procedures):

They must not be used in any context that does either of the following:

A pure procedure must not contain the following:

A pure procedure can be used in contexts where other procedures are restricted; for example:

If a procedure is used in any of these contexts, its interface must be explicit and it must be declared pure in that interface.

Example

Consider the following:

PURE FUNCTION DOUBLE(X)
  REAL, INTENT(IN) :: X
  DOUBLE = 2 * X
END FUNCTION DOUBLE

The following shows another example:

PURE INTEGER FUNCTION MANDELBROT(X)
  COMPLEX, INTENT(IN) :: X
  COMPLEX__:: XTMP
  INTEGER__:: K
  ! Assume SHARED_DEFS includes the declaration
  ! INTEGER ITOL
  USE SHARED_DEFS

  K = 0
  XTMP = -X
  DO WHILE (ABS(XTMP) < 2.0 .AND. K < ITOL)
    XTMP = XTMP**2 - X
    K = K + 1
  END DO
  MANDELBROT = K
END FUNCTION

The following shows the preceding function used in an interface block:

INTERFACE
  PURE INTEGER FUNCTION MANDELBROT(X)
    COMPLEX, INTENT(IN) :: X
  END FUNCTION MANDELBROT
END INTERFACE

The following shows a FORALL construct calling the MANDELBROT function to update all the elements of an array:

FORALL (I = 1:N, J = 1:M)
  A(I,J) = MANDELBROT(COMPLX((I-1)*1.0/(N-1), (J-1)*1.0/(M-1))
END FORALL

See Also