gil*_*ofb 5 fortran module subroutine optional-arguments
我对PRESENT
使用Fortran 95 的语句有疑问.目前我正在使用Silverfrost的Plato和他们的FTN95编译器(在"Release Win32"模式下).我想要做的是创建一个子程序SUB(a,b)
,其中b
是一个可选变量.到目前为止一切都那么好,但是当我试图给出一个新的价值时,问题就出现b
了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
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)
您不能使用不存在的变量,因此需要一种方法,例如辅助局部变量.