2

I have read that depending on the value of a function-result variable is nonstandard Fortran.

integer function my_sum(x, n)
  integer, dimension(n), intent(in) :: x
  integer              , intent(in) :: n

  integer :: i

  my_sum = 0
  do i = 1, n
    my_sum = my_sum + x(i) ! Error! Cannot re-use my_sum
  end do
end function

Instead we must use a local variable and assign it at the end of the function. To be clear, I'm not expecting the value to be saved between calls, but within the function body as if it were a local variable.

Is this statement true in Fortran 90 or later? It works on my compiler (Intel) but I'm not sure if this is standard and haven't found anything besides random university slides online.

Edit: I found the source for the function name thing. It's in Slide 4 of this presentation linked to from the official fortran-lang.org site.

Somewhere in a function there has to be one or more assignment statements like this:

function-name = expression

where the result of expression is saved to the name of the function.

Note that function-name cannot appear in the right-hand side of any expression.

Adam
  • 817
  • 8
  • 16
  • 1
    [Another question](https://stackoverflow.com/q/18146622/3157076) address the value of the loop index, so I've removed that question from here to focus on a single question. – francescalus Sep 16 '22 at 13:18
  • 1
    Can you say where you read that (false) statement? There could be some useful context in whatever wrote that we can address to remove any source of confusion. – francescalus Sep 16 '22 at 13:19
  • @francescalus Thanks for the link. I edited my question to include my source (I couldn't find it for a while) – Adam Sep 16 '22 at 13:31

2 Answers2

5

I don't think that what you read is true ...

For the function result variable, which unless your program has made other provisions, has the same name as the function itself, the latest version of the language standard that I have to hand (BS ISO/IEC 1539-1:2018 15.6.2.2 Note 1) states:

The function result is similar to any other entity (variable or procedure pointer) local to a function subprogram. Its existence begins when execution of the function is initiated and ends when execution of the function is terminated. However, because the final value of this entity is used subsequently in the evaluation of the expression that invoked the function, an implementation might defer releasing the storage occupied by that entity until after its value has been used in expression evaluation.

I interpret that to mean that your compiler is correct to compile the code. I've also used the result variable in ways such as your codes shows and cannot recall ever encountering a problem with such usage.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
5

The phrase

Note that function-name cannot appear in the right-hand side of any expression.

is either confused or confusing. Expressions don't in any meaningful sense have a "right-hand side". From the question's example the expression in the assignment within the loop is my_sum + x(i) and that's on the right-hand side of the assignment.

Leaving that to one side, there appears to be something else missing. The restriction in the text is most likely referring to a restriction on recursion. Consider (the incomplete)

function f(n)
  f = f(n-1)
end function

Here the name f cannot be referenced as a function. You'd need to use a result clause to separate the name of the function result and the function itself.

As High Performance Mark states (and I won't repeat in detail), there's no restriction on being able to define the function result more than once, or to reference it once it has been defined.


Note also that the first part of quoted statement is not true:

Somewhere in a function there has to be one or more assignment statements like this:

function-name = expression

where the result of expression is saved to the name of the function.

The function result must be defined, but (even in Fortran 90) there are other ways to define a variable without it appearing in an assignment statement. As a personal recommendation I'd avoid relying on this document given its lack of precision and stated intention of relating to the horribly old Fortran 90 standard. Modern Fortran is a much nicer beast.

francescalus
  • 30,576
  • 16
  • 61
  • 96
  • 3
    Having looked at the linked document, I'd certainly recommend not to read it: it seems to state some (frankly quite bizarre) personal preferences as rules which don't exist even in F90. – francescalus Sep 16 '22 at 13:54