What is the purpose of result variables in Fortran?

Fortran has two standard ways to return a result from a function. The first of these is the assignment of the return value of the function to the name of the function.

function foo()
    integer :: foo

    foo = 10
end function foo

The second form, standardized in Fortran 90, is implemented through the variable "result".

function foo result(res)
    integer :: res

    res = 10
 end function foo

Calling any form of function returns 10. My question is, what was the basis for the Fortran 90 committee to introduce result variables? Did they standardize common practice? Or they allowed programs to be more modular without associating a function name with a function result. For example, in the second version, foo()the function name foo()can be changed to bar(), and the function will work as expected when called.

. - , ?

+4
1

result . , , .

, , result. 1 result , , . , .

, foo foo, :

recursive function foo(n)
  foo = foo(n-1) ! Oh dear
end function

result ,

recursive function foo(n) result(res)
  res = foo(n-1) ! Yay
end function

[1] , Fortran 2008, . Fortran term.

+8

All Articles