0

In Fortran, as far as I could understand, one can declare a function in two different ways:

INTEGER FUNCTION my_func()
    (...)
END FUNCTION

or:

FUNCTION my_func()
    INTEGER :: my_func
    (...)
END FUNCTION

After some searching and reading, I'm still not sure if these two ways are equivalent or if they have some subtle differences. Some lights shedding on this would be much appreciated!

  • 1
    They're equivalent, but see https://stackoverflow.com/questions/43768605/function-result-has-no-implicit-type/43769708#43769708 and no doubt a number of other Qs and As around here. – High Performance Mark Aug 01 '22 at 12:19
  • 1
    There are other subtle parts beyond the linked answer by High Performance Mark, but that's a good starting point: the remaining subtleties are _very_ subtle and probably more confusing than would help you immediately. If you really do care about those (for interest, with the understanding that you may get lost) please do comment here and we can find further resources for you. – francescalus Aug 01 '22 at 12:33
  • Thanks @HighPerformanceMark and @Francescalus! So, for all it's worth, I can use either one and I should expect the same result (i.e. that the function returns the same data type, independently of declaring it as #1 or #2 in the example above)? And which is one is the preferred/common one by the Fortran community? – user18371559 Aug 01 '22 at 13:33
  • 1
    Both forms here have the same effect. As in other questions, which is "preferred" or "common" is a bit too subjective, but as a beginner if you stick to the form "integer :: my_func` you'll never go wrong and can easily move to the more general cases like `integer, allocatable :: my_func(:)` which can't happen with `integer function my_func()`. – francescalus Aug 01 '22 at 13:48

0 Answers0