Problem with fortran 90 Present() function

  • Context: Fortran 
  • Thread starter Thread starter sghan
  • Start date Start date
  • Tags Tags
    Fortran Function
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 7K views
sghan
Messages
10
Reaction score
1
Hi all,
When I use Present(x) to check for the presence of an optional variable, I am getting TRUE even when the subroutine call clearly does NOT contain the optional variable. See here the call to test_present(a,b):

program options
implicit none

real*8 a,b
real*8 c
a = 10.d0
b = 5.d0
c = 3.d0
call test_present(a,b)

end program options

! In a separate file:
subroutine test_present(a,b,c)
implicit none

real*8 a,b
real*8, optional :: c
logical value

value = present(c)
write(*,*)"is c present?", value !THIS RETURNS TRUE!

end subroutine test_present


Why does present(c) return TRUE even though c is not given in the call? Strangely, if variables a,b are of type integer, present(c) returns false as it should.
Thanks!
 
Physics news on Phys.org
Thanks. Found the answer: in the program above, I'd have to put the subroutine in a module, then "use module" in the program. Only then does Present(x) work properly. If anyone wants to explain why this is necessary, I would appreciate it!
 
  • Like
Likes   Reactions: BvU