相关疑难解决方法(0)

Fortran 90存在可选参数

我不理解使用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个非可选参数调用子程序,而不是任何可选参数.我无法理解为什么它会以这种方式运行,这导致我正在处理的程序中的一个主要错误.我很欣赏任何见解.谢谢.

fortran optional-parameters fortran90

9
推荐指数
1
解决办法
8516
查看次数

如果可选参数不是PRESENT,我们可以避免创建局部变量吗?

我对PRESENT使用Fortran 95 的语句有疑问.目前我正在使用Silverfrost的Plato和他们的FTN95编译器(在"Release Win32"模式下).我想要做的是创建一个子程序SUB(a,b),其中b是一个可选变量.到目前为止一切都那么好,但是当我试图给出一个新的价值时,问题就出现bif (.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
Run Code Online (Sandbox Code Playgroud)

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

if (present(b)) then
  b_aux=b
  else
    b_aux=0
endif
Run Code Online (Sandbox Code Playgroud)

fortran module subroutine optional-arguments

5
推荐指数
1
解决办法
1495
查看次数