是否有必要在将可选参数传递给另一个可选参数之前检查它? [英] Is it necessary to check an optional argument before passing it to another optional argument?
问题描述
关于可选参数的使用,我有以下问题.假设我在模块 m_aaa
I have the following question concerning the usage of optional argument. Let's say I have the following routine aaa
defined in module m_aaa
MODULE m_aaa
SUBROUTINE aaa(a, b)
INTEGER :: a
INTEGER, OPTIONAL :: b
END SUBROUTINE
END MODULE
现在我有了第二个使用模块 m_aaa
的例程.是否可以像这样传递可选参数
now I have a second routine that uses the module m_aaa
. Is it possible to pass the optional argument like this
! Variant 1:
SUBROUTINE bbb(c, d)
USE m_aaa
INTEGER :: c
INTEGER, OPTIONAL :: d
CALL aaa(c,d)
END SUBROUTINE
或者是否有必要像这样检查可选参数 d 的存在:
or is it necessary to check the presence of the optional argument d like this:
! Variant 2:
SUBROUTINE bbb(c, d)
USE m_aaa
INTEGER :: c
INTEGER, OPTIONAL :: d
IF (PRESENT(d)) THEN
CALL aaa(c,d)
ELSE
CALL aaa(c)
ENDIF
END SUBROUTINE
感谢您的帮助.
推荐答案
在将可选虚拟参数作为实际参数传递给另一个可选虚拟参数之前,无需检查它是否存在.
It is not necessary to check to presence of an optional dummy argument before passing it as an actual argument to another optional dummy argument.
12.5.2.12 第 4 段(ISO/IEC 1539-1(2010 年 6 月 7 日草案)又名 Fortran 2008)允许关于不存在的可选实际参数:
This is allowed by 12.5.2.12 paragraph 4 (ISO/IEC 1539-1 (Draft 7 June 2010) aka Fortran 2008) regarding optional actual arguments that are not present:
除了上面列表中的说明外,它可以作为对应于可选虚拟对象的实际参数提供参数,然后也被认为不存在.
Except as noted in the list above, it may be supplied as an actual argument corresponding to an optional dummy argument, which is then also considered not to be present.
这篇关于是否有必要在将可选参数传递给另一个可选参数之前检查它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!