[comp.lang.fortran] Keyword and optional argments

maine@elxsi.dfrf.nasa.gov (Richard Maine) (11/30/90)

On 30 Nov 90 02:21:55 GMT, whit@milton.u.washington.edu (John Whitmore) said:

John> 	Finally, a topic I can really get behind!  Most languages have
John> some constructs that allow optional arguments: 

John> 	OPEN(UNIT= the_unit, FILE='FileName',FORM='UNFORMATTED)

John> is a Fortran example.  Yet, this sort of 'keyword=value' calling
John> sequence is not supported for user subroutines.  The language implementors
John> needed it; SO DO THE USERS.  

John> 	CALL READ_THE_FILE( file='FileName', 
John> 	&	format='Bruce''s third generation data set', 
John> 	&	into= data_array(5)   )

I 100% agree.  And I note that this is incorporated in Fortran 90
modules, one of my favorite Fortran 90 features.  The statement above
is a perfectly legal Fortran 90 statement (well, except that you'd
probably be using free source form, which does continuation lines
differently).  The subroutine interface might be something like.

   module file_stuff

   use precision

   contains
      subroutine read_the_file(file,format,some_other_argument,into)

        character, intent(in) :: file*(*)
        character, intent(in), optional :: format*(*)
        integer, intent(in), optional :: some_other_argument
        real(r_kind), intent(out) :: into
       ........
      end subroutine read_the_file
   end module file_stuff

where for no relevant reason, except that I like it, I've also choosen
to illustrate the usage of the KIND parameter.  The r_kind parameter
above might be defined in

  module precision

     integer, parameter :: r_kind = selected_real_kind(12,30)
     ......

  end module precision

Selected_real_kind is a Fortran 90 intrinsic that gives the
"kind" of the smallest (loosely speaking) real type with
precision of at least 12 decimal digits and range of at least
10**-30 to 10**+30.  Thus the into variable is double precision
(to use old style Fortran terms) on my Sun or other 32-bit
systems, but single precision on the Cybers or Crays; no source
code changes required.

I think I've got all the above correct, though I'm still a bit
short on compilers to test my Fortran 90 code on.  (Waiting
anxiously for you Keith.  Either a Sun3 or Sparc version will
do fine. :-))

--

Richard Maine
maine@elxsi.dfrf.nasa.gov [130.134.64.6]

acmeyer@hpfcso.HP.COM (Alan C. Meyer) (12/02/90)

>I 100% agree.  And I note that this is incorporated in Fortran 90
>modules, one of my favorite Fortran 90 features.

To elaborate a little (and be a little picky?), keyword arguments can
be used in F90 for any procedure which has an "explicit interface".  In
F90, this means any intrinsic procedure, module procedure, or internal
procedure can always be invoked with keyword arguments.  In addition,
it also includes any external procedure for which an interface block is
accessible.

So, to build on the given example, an external procedure "read_the_file"
could also be called with keyword args like so -

	subroutine get_ready
	  ...
	interface
           subroutine read_the_file(file,format,some_other_argument,into)
	   use precision
           character, intent(in) :: file*(*)
           character, intent(in), optional :: format*(*)
           integer, intent(in), optional :: some_other_argument
           real(r_kind), intent(out) :: into
           end subroutine read_the_file
	end interface
	   ...
	call read_the_file(into=some_array, file='...')


Alan Meyer
Colorado Language Lab
Hewlett-Packard