Fortran 90 Presence Of Optional Arguments

2020-03-14 22:28发布

I do not understand the behavior of the present() intrinsic function with pgf90 7.2. I wrote a 20 line sample program to test this, but the results still make no sense to me. Observe:


subroutine testopt(one,two,three,four,five)

  implicit none

  integer, intent(in) :: one,two
  integer, intent(out) :: three
  integer, intent(in), optional :: four
  integer, intent(out), optional :: five

  three = one + two

  print *,"present check: ",present(four),present(five)

  if (present(four) .and. present(five)) then

  five = four*four

end if

end subroutine testopt

if I: call testopt(1,2,(any variable)) from my main program, it prints: "present check: T F". However if I: call testopt(1,2,(any variable)) from a subprogram it prints: "present check: T T". I expected to see "present check: F F" in either case, because I am only calling the subroutine with the 3 non-optional arguments, and neither of the optional ones. I cannot fathom why it would behave this way, and this is causing a major bug in a program I am working on. I appreciate any insight. Thanks.

1条回答
一夜七次
2楼-- · 2020-03-14 22:49

Are you placing this subroutine in a module and then having a "use" statement for that module in the calling routine (main program or subroutine)? A typical rule is that many of the advanced / new features of Fortran 90 require an explicit interface so that both the caller and callee pass the arguments consistently. The easiest and best way to accomplish this is with module / use. Just a guess...

查看更多
登录 后发表回答