Re: optional arguments in subroutine or using generic procedure



Mike wrote:
Hi,

I want to write a subroutine with flexible numbers of arguments. One
of the argument 'ID' will decide how many arguments this subroutine
would have. If ID=1, fewer arguments. If ID=2, add many additional
arguments. I am pondering if I have to write two subroutines with
different names and using generic procedure, or I write only one
subroutine with optional arguments declared. Currently, I've chosen
using generic procedure. However, it doesn't seem beneficially enough,
but only with length interface and a generic name only.

Hello,

So why use generic procedure?

If you have a task/algorithm that you want to put in a procedure and use it with arguments of different types,
integer, real, complex, etc
different kinds,
integer(short), integer(long), real(single), complex(double), etc..
and/or different ranks,
integer, intent(in) :: i(:)
integer, intent(in) :: i(:,:)
integer, intent(in) :: i(:,:,:)
integer, intent(in) :: i(:,:,:,:)
...etc...

In the short description you gave above, the one-or-many argument case might be simpler if you can do

call mysub(inarg1, inarg2=inarg2, outarg)

where arg2 is a structure containing all the many additional arguments you mention. The call could be, e.g.
subroutine mysub(inarg1,inarg2,outarg)
real, intent(in) :: inarg1 ! or whatever type
type(myarg_type), optional, intent(in) :: inarg2
real, intent(out) :: outarg ! or whatever type

Then you only have to do a single

if (present(arg2)) then
...process additional args...
end if

rather than check if *all* of the many additional individual args are present.

It will also allow you to alter the number of additional optional arguments (by adding more components to the structure) without changing the interface. IMO, that's always a plus.

cheers,

paulv


--
Paul van Delst Ride lots.
CIMSS @ NOAA/NCEP/EMC Eddy Merckx
Ph: (301)763-8000 x7748
Fax:(301)763-8545
.



Relevant Pages

  • Re: optional arguments in subroutine or using generic procedure
    ... I want to write a subroutine with flexible numbers of arguments. ... different names and using generic procedure, ... more components to the structure) without changing the interface. ... I only have to check one derived type by using ...
    (comp.lang.fortran)
  • optional arguments in subroutine or using generic procedure
    ... I want to write a subroutine with flexible numbers of arguments. ... I am pondering if I have to write two subroutines with ... different names and using generic procedure, ... but only with length interface and a generic name only. ...
    (comp.lang.fortran)
  • Re: compiler switch -c
    ... All methods must be exposed by interface. ... module procedure fruit_summary_ ... end subroutine init_fruit_ ... character, intent, optional:: message ...
    (comp.lang.fortran)
  • TRANSFER arbitrary data ? (long)
    ... subroutine to minimize a function of n variables, ... end interface ... that to an integer array and back. ... subroutine minimize(Func, x, context) ...
    (comp.lang.fortran)
  • Re: Are procedure dummy arguments ignored in generic procedure resolution?
    ... the interface block that defines the ... recursive subroutine step_up ... Compiling program unit mykinds at line 1: ...
    (comp.lang.fortran)