Silverfrost Forums

Welcome to our forums

Incorrect result of Present

16 May 2022 10:01 #28979

FTN95 fails to identify that the optional argument, i, in f2 is not present. I believe that the code is invalid Fortran, and that the External declaration in line 3 needs to be replaced by the commented-out Interface. As it is, FTN95 indicates that the optional argument, i, is present, whereas other compilers generate a run-time error. The argument i is correctly indicated as not present if the interface block is used. !

Module m1
Contains
 Function f1(f)
   Integer :: f1
   Integer, External :: f
!   Interface
!    Function f(i)
!     Integer :: f
!     Integer, Intent(In), Optional :: i
!    End Function f
!   End Interface
!
   f1 = f()
   Return
 End Function f1
End Module m1
!
Module m2
Contains
 Function f2(i)
   Integer :: f2
   Integer, Intent(In), Optional :: i
!
   print*,Present(i)
   f2 = 0
!
   Return
 End Function f2
End Module m2
!
Program p
   Use m1
   Use m2
   Integer :: i
   i = f1(f2)
End Program p
16 May 2022 10:22 #28980

Simon

I am not able to run your code at the moment.

Reading your code, it seems to me that the interface is required. Without the interface the code is not valid and the runtime outcome will be compiler dependent and maybe also random.

23 May 2022 6:01 #28991

Simon

The interface is required and (when omitted) FTN95 produces a runtime error report when /CHECK is used.

23 May 2022 1:19 #28992

Understood Paul. Thanks

Please login to reply.