Fortran 90存在可选参数

Jas*_*son 9 fortran optional-parameters fortran90

我不理解使用pgf90 7.2的present()内在函数的行为.我写了一个20行的示例程序来测试这个,但结果对我来说仍然没有意义.注意:


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
Run Code Online (Sandbox Code Playgroud)

如果我:从我的主程序调用testopt(1,2,(任何变量)),它打印:"present check:T F".但是如果我:从子程序中调用testopt(1,2,(任何变量)),它会打印:"present check:T T".我希望在任何一种情况下都能看到"当前检查:F F",因为我只使用3个非可选参数调用子程序,而不是任何可选参数.我无法理解为什么它会以这种方式运行,这导致我正在处理的程序中的一个主要错误.我很欣赏任何见解.谢谢.

M. *_* B. 7

您是否将此子例程放在一个模块中,然后在调用例程(主程序或子例程)中为该模块设置"use"语句?一个典型的规则是Fortran 90的许多高级/新功能都需要一个显式接口,以便调用者和被调用者一致地传递参数.最简单,最好的方法是使用模块/使用.只是一个猜测......

  • 你是绝对正确的。将程序放入模块中给了我预期的响应。谢谢。 (2认同)