Numerics
Elemental Functions
Apply Per Element
An elemental function works on a scalar but can be applied element-wise to a whole array.
Program
Play the program to double every element of an array.
elemental.f90
program elemental_demo
implicit none
integer :: nums(4), doubled(4)
nums = [1, 2, 3, 4]
doubled = times_two(nums)
print '(I0)', sum(doubled)
contains
elemental function times_two(x) result(y)
integer, intent(in) :: x
integer :: y
y = x * 2
end function times_two
end program elemental_demo
elemental
`elemental function` applies to each element of an array argument.
scalar definition
The body is written for a scalar, but works on arrays too.
intent(in)
Elemental arguments are read-only.