问题
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
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
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
Thanks for your help.
回答1:
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.
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.
来源:https://stackoverflow.com/questions/26367505/is-it-necessary-to-check-an-optional-argument-before-passing-it-to-another-optio