gpt4 book ai didi

module - 如果可选参数不存在,我们可以避免创建局部变量吗?

转载 作者:行者123 更新时间:2023-12-04 17:50:11 25 4
gpt4 key购买 nike

我在使用 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

是否有解决这种情况的优雅方法,或者我是否真的需要创建另一个变量, b_aux例如,然后使用以下代码?:
if (present(b)) then
b_aux=b
else
b_aux=0
endif

最佳答案

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

关于module - 如果可选参数不存在,我们可以避免创建局部变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18302083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com