Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

IMPURE

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

Description

This kind of procedure is specified by using the prefix IMPURE in a FUNCTION or SUBROUTINE statement. By default, all user-defined procedures are impure, that is, they are allowed to have side effects, except for elemental procedures, which are by default PURE.

An IMPURE elemental procedure has the restrictions that apply to elemental procedures, but it does not have any of the restrictions of PURE elemental procedures.

An impure elemental procedure can have side effects and it can contain the following:

  • Any external I/O statement (including a READ or WRITE statement whose I/O unit is an external file unit number or *)

  • A PAUSE statement

  • A STOP statement or an ERROR STOP statement

  • An image control statement

An impure elemental procedure cannot be referenced in a context that requires a procedure to be pure; for example:

  • It cannot be called directly in a FORALL statement or be used in the mask expression of a FORALL statement.

  • It cannot be called from a pure procedure. Pure procedures can only call other pure procedures, including one referenced by means of a defined operator, defined assignment, or finalization.

  • It cannot be passed as an actual argument to a pure procedure.

Example

module my_rand_mod
integer, save :: my_rand_seed (8)

  contains
    impure elemental subroutine my_rand (r)

    ! my_rand updates module variable my_rand_seed (an array)
    ! and returns the next value of a "pseudo" random sequence in
    ! output dummy argument r

      real, intent(out) :: r

      ! code goes here
      
    end subroutine my_rand
end module my_rand_mod

See Also