6

我对PRESENTFortran 95 的语句有疑问。目前我正在使用 Silverfrost 的 Plato 和他们的 FTN95 编译器(在“Release Win32”模式下)。我想做的是创建一个 subroutine SUB(a,b),其中b是一个可选变量。到目前为止一切顺利,但是当我尝试为bwith赋予新值时出现问题if (.NOT. present(b)) b=0。这是代码:

module MOD
contains
  subroutine SUB(a,b)
  implicit none
  integer :: a
  integer,optional :: b
  if (.NOT. present(b)) b=0
  print*, a,b
  end subroutine SUB
end module MOD

program TEST
use MOD
implicit none

integer :: i=2, j=1

call SUB(i,j)
call SUB(i)
call SUB(j)

end program TEST

有没有摆脱这种情况的优雅方法,或者我真的需要创建另一个变量,b_aux例如,然后使用以下代码?:

if (present(b)) then
  b_aux=b
  else
    b_aux=0
endif
4

1 回答 1

11

您不能使用不存在的变量,因此需要诸如辅助局部变量之类的方法。

于 2013-08-18T18:18:53.427 回答