making default arguments easier to use
- From: Beliavsky <beliavsky@xxxxxxx>
- Date: Fri, 27 Jul 2007 11:51:50 -0700
Here is a "discovery" I made. I used to write a lot of "boilerplate"
code such as
if (present(max_rows)) then
max_rows_ = max_rows
else
max_rows_ = 100000
end if
where max_rows is an optional argument. Nowadays I replace the 5 lines
above with just
max_rows_ = default(100000,max_rows)
where default is an interface to a set of functions such as as
elemental function default_integer(def,opt) result(ii)
! return opt if it is present, otherwise def (integer arguments and
result)
integer, intent(in) :: def
integer, intent(in), optional :: opt
integer :: ii
if (present(opt)) then
ii = opt
else
ii = def
end if
end function default_integer
(similar functions have been written for other types). Using this
technique makes the procedures with optional arguments shorter and
easier to read IMO. It uses the feature of Fortran that allows non-
present optional arguments to be passed to procedures.
.
- Prev by Date: Re: forall and do loop
- Next by Date: Re: forall and do loop
- Previous by thread: How do add decoration to imported subroutines in Intel Compiler?
- Next by thread: Help linking f77 libraries into gfortran
- Index(es):