What's wrong with statement functions

I have been using statement function every now and then until I realized that they have been declared obsolete since f90 (I was a bit slow on this one). I am not sure to understand the reasons behind.

They remind me of limited lambda functions that I am used to when coding in C#. And even though limited in scope they were making the code less verbose, which, as much as I like coding in Fortran, is not its strong suit.
Just looking at this example:

integer f, i
f(i)=i
print *, f(1)

against the standard way to do it

 ...
print *, f(1)

contains

  integer function f(i)
    integer i
    f = i
  end function

Is there any drawback or performance issue with statement function that would justify their obsolescence?

Not performance, but an ugly and confusing syntax. The placement within the code may confuse compilers, or at least that is what I remember from the past. They are also very limited - a single statement by definition. Contained routines are much more general, but they do require more typing :innocent:.

I seldom use a statement function now but when I do I put ! stmt function at the end of its line, to remind me it’s not assigning a value to an array element.

I think it is just the fact that a contained procedure was deemed to be an improved way to achieve the same result, namely a function that only has scope within its host. I do not know of any intrinsic advantages that a statement function might have over a contained procedure. The converse is not true, of course, contained procedures have many many advantages over statement functions.

So nothing wrong then, just a redundant feature. Too bad because it used to be the closest thing to a lambda.
@RonShepard, no doubt about the advantages of contained functions. It’s just that sometimes they are a bit of an overkill. I could use some preprocessor macros but scoping might be an issue though. Don’t tell me they are obsolete too :scream:!

1 Like

Preprocessing of any sort has never been part of the Fortran standard (though its use is fairly common). That said, it may be part of Fortran 202Y.