How does FORTRAN interact with optional arguments?
In a calling function I have this:
call ESMF_TimeGet( date, yy=year, mm=month, dd=day, s=sec, rc=rc)
The signature for ESMF_TimeSet
is:
! ESMF_TimeGet - Get value in user-specified units
subroutine ESMF_TimeGet(time, YY, YRl, MM, DD, D, Dl, H, M, S, Sl, MS, &
US, NS, d_, h_, m_, s_, ms_, us_, ns_, Sn, Sd, &
dayOfYear, dayOfYear_r8, dayOfYear_intvl, &
timeString, rc)
type(ESMF_Time), intent(in) :: time
integer, intent(o开发者_Go百科ut), optional :: YY
integer(ESMF_KIND_I8), intent(out), optional :: YRl
integer, intent(out), optional :: MM
integer, intent(out), optional :: DD
integer, intent(out), optional :: D
integer(ESMF_KIND_I8), intent(out), optional :: Dl
integer, intent(out), optional :: H
integer, intent(out), optional :: M
integer, intent(out), optional :: S
integer(ESMF_KIND_I8), intent(out), optional :: Sl
integer, intent(out), optional :: MS
integer, intent(out), optional :: US
integer, intent(out), optional :: NS
double precision, intent(out), optional :: d_
double precision, intent(out), optional :: h_
double precision, intent(out), optional :: m_
double precision, intent(out), optional :: s_
double precision, intent(out), optional :: ms_
double precision, intent(out), optional :: us_
double precision, intent(out), optional :: ns_
integer, intent(out), optional :: Sn
integer, intent(out), optional :: Sd
integer, intent(out), optional :: dayOfYear
real(ESMF_KIND_R8), intent(out), optional :: dayOfYear_r8
character (len=*), intent(out), optional :: timeString
type(ESMF_TimeInterval), intent(out), optional :: dayOfYear_intvl
integer, intent(out), optional :: rc
type(ESMF_TimeInterval) :: day_step
integer :: ierr
When I'm calling ESMF_TimeSet
how is the subroutine able to convert the yy=yr
argument into a yy
variable inside the subroutine (and similarly for mm
and dd
)? Also, does FORTRAN care about case-sensitivity of variables?
how is the subroutine able to convert the "yy=yr" argument into a yy variable inside the subroutine?
There is no yy
variable in your procedure. Only yy
dummy argument. When you call this procedure you use so-called named arguments. This feature is not specific to Fortran. But what's your problem with optional arguments?
And the same for mm and dd. Does FORTRAN care about case-sensitivity of variables?
Fortran is case-insensitive language. So no, it doesn't care.
Okay. I'll try to explain some basics because I have some difficulties with understanding your comments. :)
When it comes to procedures the Fortran terminology differs from the "common" one in some respects:
- It's common to distinguish between procedures (performs some tasks returning several results through arguments) and functions (invokes within expression and return a single value which is used in expression) while collectively calling them subroutines. In Fortran we have subroutines and functions collectively calling procedures.
- While calling procedure you pass some info through arguments. Commonly entities appeared at procedure definition are called formal arguments and entities appeared in procedure call are called actual arguments. In Fortran formal arguments are called dummy arguments. Be aware, because often dummy arguments are referred to as meaningless arguments which are used only to conform the API, i.e. EmptyParam in Delphi.
When procedure is called (is referenced in terms of Fortran standard)
the actual argument list identies the correspondence between the actual arguments and the dummy arguments of the procedure.
So basically the correspondence is by position. Some languages (including Fortran) also have the ability to establish correspondence by keyword. The long story short:
The keywords are the dummy argument names and there must be no further positional arguments after the first keyword argument.
(c) Michael Metcalf, John Reid, Malcolm Cohen. Modern Fortran Explained.
For details see Fortran Standard (you can grab the copy the final draft of the Fortran 2008 standard by the link provided here), 12.5.2 Actual arguments, dummy arguments, and argument association.
So keyword arguments is a feature known as named arguments or named parameters in other languages.
But this feature is not the only one which can help programmer to write concise and readable code for calling procedures without overloading. Some languages also have default arguments. Fortran has similar feature: so-called optional arguments. It looks a bit different but the aim is the same.
Keyword arguments are often used in conjunction with optional arguments. For example, you should use keyword arguments when you leave out optional arguments in the middle of the argument list.
So what's your question is?
精彩评论