Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

fortran - Is it necessary to check an optional argument before passing it to another optional argument?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...