Clicky

Fortran Wiki
elemental

Elemental Procedures

Elemental functions are defined as scalar operators, with a single scalar dummy argument and a scalar return value, but they may be invoked with arrays as actual arguments in which case the function will be applied element-wise, with a conforming array return value. Such functions must also be pure functions, with no side effects, and can be indicated with the elemental prefix. Since it is implied that an elemental function is also pure, the pure prefix is not necessary.

Elemental subroutines may be defined in a similar way, however, side effects are permitted for intent(out) or intent(inout) arguments.

The main benefit of elemental procedures is that advance knowledge that a function is elemental simplifies parallel execution.

Example

module test_mod
  implicit none
contains
  elemental real function square(x)
    real, intent(in) :: x
    square = x*x
  end function
end module

program test_prog
  use test_mod
  implicit none

  real, dimension(3) :: x = (/ 1.0, 2.0, 3.0 /)

  print *, square(x)
end program