Passing of different types to same parameter of a subroutine 
Author Message
 Passing of different types to same parameter of a subroutine

Hi!

How can I let a function accept arguements of different type for the
same parameter?
For example just a single real-valued variable, or a whole 2D grid.
I'd like to handle the different situations within the function/
subroutine
and not have to write new functions for each different type.

Arjan



Fri, 26 Aug 2011 03:30:40 GMT  
 Passing of different types to same parameter of a subroutine

Quote:

> How can I let a function accept arguements of different type for the
> same parameter?
> For example just a single real-valued variable, or a whole 2D grid.
> I'd like to handle the different situations within the function/
> subroutine
> and not have to write new functions for each different type.

Do you really mean "functions" or are you using that term to mean any
kind of procedure? Not that it makes a lot of difference, as the same
principles apply to functions and subroutines.

You aren't going to find a handy way to handle that. There are two
approaches, but I don't think either of them is going to do what you
appear to want.

1. See generic procedures. They can make it look like a single procedure
from the calling end. But you do end up having to write out each of the
individual procedures, which is what you said you don't want to do.
Sometimes one can play with various preprocessing tricks to simplify
writing multiple procedures with almost identical source text. But
that's not likely to work well with something as disparate as handling a
single scalar versus an array.

Say, that reminds me. Make that 3 ways, and I'll list the 3rd one here
as number 2, because it ties in with the above comment.

2. See elemental procedures. They do not handle different types. But
your example didn't use different types, so maybe you didn't mean the
word "type" literally. Elemental procedures do handle different ranks,
as in scalar versus 2-D. There are rather severe restrictions on
elemental procedures. I can't tell whether your needs would fit within
the restrictions, but your description doesn't rule it out.

3. See polymorphism in f2003. That can handle multiple types for a
single variable. That's what the word "polymorphism" means. ("Poly" for
"multiple" and "morph" for "form", so it is "multiple forms"). But

a. It requires an f2003 compiler, or at least an f95 one with that
rather large f2003 feature as an extension.

b. It can require quite a bit of work to set up... as well as quite a
bit of learning the concepts involved. If you just jump in without
spending a bit of time learning about the concepts, you are almost
guaranteed to do a poor job.

c. It doesn't apply (much) to intrinsic types such as real. You can
possibly use an unlimited polymorphic variable, I suppose... maybe.
Somewhat of a variant of that would be to use a C pointer. C void
pointers have some similar capabilities and are probably supported by
more of today's fortran compilers.

All of these possibilities are moderately complicated. I'm not at all
sure they will meet your needs or work out better for you than just
writing multiple procedures.

--
Richard Maine                    | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle           |  -- Mark Twain



Fri, 26 Aug 2011 04:47:05 GMT  
 Passing of different types to same parameter of a subroutine

Quote:
> All of these possibilities are moderately complicated. I'm not at all
> sure they will meet your needs or work out better for you than just
> writing multiple procedures.

Thanks, Richard. Your explanations is (as always) thorough.
An example of what I am looking for would be a routine to
estimate the height for a location on earth, or for a whole grid of
points:

TYPE(Vector2) :: x,h
TYPE(Vector2Grid) :: WesternEurope,EuropeanHeight
...
h = EstimateHeight(x)
EuropeanHeight = EstimateHeight(WesternEurope)

In this case, the output type should follow the input type.
Maybe in this case "generic procedures" could do it: the grid version
calling the single-point version. In real life however, I have more
than one parameter for my function/subroutine (you were right) and I
want
to be prepared to accept any of them as either single-valued parameter
or as grid-version, e.g. a routine to estimate the local UV-dose,
as a function of the thickness of the ozone layer, cloudiness,
aerosol-concentration, height etc.:

MyUVDose = EstimateUV(x,Ozone,Clouds,Aerosols,Height)

where any of the arguements can be a either a single scalar or a grid.

Arjan



Fri, 26 Aug 2011 05:55:24 GMT  
 Passing of different types to same parameter of a subroutine

Quote:
> > All of these possibilities are moderately complicated. I'm not at all
> > sure they will meet your needs or work out better for you than just
> > writing multiple procedures.

Arjan,

You might find the papers by Viktor Decyk on OOP in F95 useful:

http://exodus.physics.ucla.edu/Fortran95/PSTIResearchLecSeries1.html

Paul



Fri, 26 Aug 2011 13:41:36 GMT  
 Passing of different types to same parameter of a subroutine

Quote:

> > > All of these possibilities are moderately complicated. I'm not at all
> > > sure they will meet your needs or work out better for you than just
> > > writing multiple procedures.

> Arjan,

> You might find the papers by Viktor Decyk on OOP in F95 useful:

> http://exodus.physics.ucla.edu/Fortran95/PSTIResearchLecSeries1.html

> Paul

Also, if you'd like something in book form, Ed Akin's book Object-
Oriented Programming via Fortran 90/95 has some good material on
emulating polymorphism in Fortran 90/95 (very similar in content to
Decyk's publications).

The developmental branch of gfortran (4.4.0) supports polymorphism in
a slightly non-standard way (with standard-conforming polymorphism on
the agenda for verion 4.5 I believe).  For a list of commercial
compiler support as of December 2008, see http://portal.acm.org/citation.cfm?id=1462614.1462617.

Damian



Sat, 27 Aug 2011 06:47:05 GMT  
 Passing of different types to same parameter of a subroutine
Thanks, Damian and Paul, I'll have a look!

A.



Sat, 27 Aug 2011 19:39:14 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. pass a subroutine (use derived data type) as an argument to another subroutine

2. Passing parameters to external subroutines

3. newbie: passing parameters to subroutine

4. Passing array of derived data types to a subroutine

5. DVF: Passing mixed arg types to subroutines?

6. Passing pointers to derived types to subroutines

7. passing pointer of different type to one procedure

8. Passing arrays of different types

9. passing a task type as a generic parameter

10. VB 5.0 and Inter-language Data Type Conversions (COBOL) and Parameter Passing

11. To assign different colors to different values of a set parameter

12. passing functions or subroutines in a subroutine

 

 
Powered by phpBB® Forum Software