Re: optional arguments in subroutine or using generic procedure
- From: "Mike" <acout@xxxxxxx>
- Date: 13 Jul 2006 17:19:56 -0700
Paul Van Delst wrote:
Mike wrote:thanks. I like this. I only have to check one derived type by using
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
it to wrap the additional arguments.
Mike
.
- References:
- optional arguments in subroutine or using generic procedure
- From: Mike
- Re: optional arguments in subroutine or using generic procedure
- From: Paul Van Delst
- optional arguments in subroutine or using generic procedure
- Prev by Date: Re: gfortran/g77 ld problem: undefined references
- Next by Date: Re: recl on g95 and gfortran
- Previous by thread: Re: optional arguments in subroutine or using generic procedure
- Next by thread: Re: optional arguments in subroutine or using generic procedure
- Index(es):
Relevant Pages
|