Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Problem with fortran 90 Present() function

  1. Jun 24, 2010 #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 seperate 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.
  2. jcsd
  3. Jun 24, 2010 #2


    Staff: Mentor

    There are some examples and discussion here. Hope this helps.
  4. Jun 28, 2010 #3
    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!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook