Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran elemental functions vs elemental subroutines

Fortan allows elemental subroutines to have intent(inout) and intent(out) arguments, but elemental functions are only allowed intent(in).

Why is that? Is it just a stylistic convention, or is there something generically different about invoking functions and calling subroutines?

In other words,

Elemental Integer Function FOO(i)
  Integer, intent(in) :: i
    ...
  FOO=something
End Function

and

Elemental Subroutine FOO(i, v)
  Integer, intent(in)  :: i
  Integer, intent(out) :: v
    ...
  v=something
End Subroutine

— are these implementations of FOO equivalently efficient?

like image 247
drlemon Avatar asked Jul 29 '11 21:07

drlemon


People also ask

What is the difference between a subroutine and a function Fortran?

A function must return a single value, and can be invoked from within expressions, like a write statement, inside an if declaration if (function) then , etc. A subroutine does not return a value, but can return many values via its arguments and can only be used as a stand-alone command (using the keyword call ).

What is a pure function in Fortran?

Fortran procedures can be specified as PURE, meaning that there is no chance that the procedure would have any side effect on data outside the procedure. Only pure procedures can be used in specification expressions. The PURE keyword must be used in the procedure declaration.


1 Answers

There is no point in having an elemental subroutine without at least one argument marked as intent(out) or intent(inout), because you have to pass the result somehow. A function has its return value, a subroutine must use its arguments. In Fortran 2008 AFAIK elemental procedures doesn't have to be pure, but it's hard to imagine a useful elemental subroutine only through its side effects.

like image 126
Vladimir F Героям слава Avatar answered Oct 24 '22 18:10

Vladimir F Героям слава