Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to pass derived type argument to a subroutine

  1. Jun 27, 2012 #1
    Hi all !

    I have a derived type as:

    Code (Text):
    TYPE type_atom_type_info
            integer:: type_code
            character(len=4)::type_cname
            real(kind=dbl)::mass
        end TYPE type_atom_type_info
     
    I have an array of this type as:
    Code (Text):
    TYPE(type_atom_type_info) atom_type_info(250)
    I want to pass this array to a subroutine. I have made an interface in the same file for the subroutine

    Code (Text):
    interface
            subroutine cname_to_code(atom_type_info,cname,code)
                TYPE(type_atom_type_info),intent(in)::atom_type_info(250)
                character(len=4),intent(in)::cname
                integer,intent(out)::code
            end subroutine cname_to_code       
        end interface
     
    But I am getting an error as:
    Code (Text):
    nma.f90:234.60:

       TYPE(type_atom_type_info),intent(in)::atom_type_info(250)
                                                                1
    Error: the type of 'atom_type_info' at (1) has not been declared within the interface
    nma.f90:1496.20:

     call cname_to_code(atom_type_info,'NH1',temp_k)
                        1
    Error: Type mismatch in argument 'atom_type_info' at (1); passed TYPE(type_atom_type_info) to REAL(4)
    nma.f90:4072.26:

     TYPE(type_atom_type_info),intent(in)::atom_type_info(250)
                              1
    Error: Derived type 'type_atom_type_info' at (1) is being used before it is defined
    I tried redefining the whole TYPE in the interface, but that didn't work.
    How do I make the type 'global' ? Or is there any other way to pass derived type arguments to functions?

    Thank you in advance !!
     
  2. jcsd
  3. Jun 27, 2012 #2

    chiro

    User Avatar
    Science Advisor

    Hey garbageij.

    You will need to reference this definition in each module. In C/C++ We use include files to include definitons for the source files and also for other header files that use those definitions.

    I just searched google and I believe the statement is INCLUDE 'file'. What you should do is save your definitions to some file (all your common data types) and include this file everytime you need to use and work with data of that type.
     
  4. Jun 27, 2012 #3
    Hmm.....so I guess there's no other way, huh?
    Thanks !!
     
  5. Jun 27, 2012 #4

    chiro

    User Avatar
    Science Advisor

    You can copy the type statement and throw it in the module, but that's not a good thing to do.

    It's better to update one include file than trying to search for every single instance. Imagine if you had 20 classes that used the one definition and then imagine having to each everything 20 times!

    This is why we have include files.
     
  6. Jun 27, 2012 #5
    A couple of comments.

    Writing a separate file with such declaration and including it everywhere you need it, it's one thing...if just like that...you will simply get a local variable everywhere

    BUT,

    If you include the declaration and a common block THEN, you will get all those subroutines to use the same varaibles...even if it is not 'global'

    The Fortran 90 way of making some variables global is via a MODULE...you just create a module, put in there all the variables you need to use in your various subroutines...then, wherever you need them, instead of the "include" statement, you do "use modulename"
     
  7. Jun 28, 2012 #6
    Thank you so much for your replies !!

    So I have made a module, in which I have put the derived type as well as the array, and now it's working fine. Making a module it seems was a good idea to begin with as now some of my function take less arguments (a few arguments were common to many functions)


    Thanks again !!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook