Lahey/Fujitsu Fortran


Fortran procedures can be elemental, meaning that they work on each element of an array argument as if the argument were a scalar. The ELEMENTAL keyword must be used in the procedure declaration. Note that all elemental procedures are also pure procedures.


An ELEMENTAL procedure declaration implies that the procedure may be called using scalar or array arguments.


ELEMENTAL SUBROUTINE sub-name([dummy-args])
ELEMENTAL [type-spec] FUNCTION func-name([dummy-args]) [RESULT(result-name)]

sub-name is the name of the subroutine.

dummy-args is a comma-separated list of dummy argument names.

type-spec is

INTEGER [kind-selector] REAL [kind-selector] DOUBLE PRECISION COMPLEX [kind-selector] CHARACTER [char-selector] LOGICAL [kind-selector] TYPE (type-name)

kind-selector is ( [KIND=] kind )

char-selector is ( [LEN=] length [, ) KIND=kind])

or (KIND=kind [, LEN=length]) or * char-length [,]

kind is a scalar INTEGER expression that can be evaluated at compile time.

length is a scalar INTEGER expression or *

char-length is a scalar INTEGER literal constant or (*)

func-name is the name of the function.

result-name is the name of the result variable.


Declaring a procedure to be ELEMENTAL also implies that the procedure is PURE. Elemental procedures are subject to all the restrictions of a PURE Procedure.

All dummy arguments and function results must be scalar, and cannot have the POINTER attribute, be a dummy procedure, or an alternate return.

Dummy arguments may not appear in a specification statement, except as an argument to one of the following functions: BIT_SIZE, KIND, LEN, DIGITS, EPSILON, HUGE, MAXEXPONENT, MINEXPONENT, PRECISION, RANGE, RADIX or TINY.

When calling an elemental procedure, all actual arguments must be conformable to each other.

Dummy arguments of elemental functions must have the INTENT(IN) attribute.

If any actual argument to an elemental subroutine is an array, all INTENT(OUT) and INTENT(IN OUT) arguments must also be an array and all arrays must be conformable.


If all actual arguments to an elemental function are scalar, the result is scalar.

If any actual argument is an array, the result is an array and conformable with the array argument. The resulting array contains the value of the scalar operation performed on each element of the array.


module mod1 ! gives us an implicit interface contains elemental function elefun1(a,b) integer :: elefun1 integer,intent(in) :: a,b elefun1=a-b end function elefun1 elemental subroutine elesub1(a,b,c) integer,intent(out) :: c integer,intent(in) :: a,b c=a-b end subroutine elesub1 end module   program prog1 use mod1 integer :: i=0,j=-1,k,ia(3)=(/1,2,3/),ib(3)=(/4,5,6/),ic(3) write(*,*) elefun1(i,j) ! writes 1.0 write(*,*) elefun1(i,ia) ! writes -1.0 -2.0 -3.0 write(*,*) elefun1(ia,i) ! writes 1.0 2.0 3.0 write(*,*) elefun1(ia,ib) ! writes -3.0 -3.0 -3.0 call elesub1(i,j,k) write(*,*) k ! writes 1 call elesub1(ia,j,ic) write(*,*) ic ! writes 2 3 4 call elesub1(i,ib,ic) write(*,*) ic ! writes -4 -5 -6 call elesub1(ia,ib,ic) write(*,*) ic ! writes -3 -3 -3 end program