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

[Fortran] Function implicit type error

  1. Jun 18, 2015 #1
    I am having an issue with the declared type of the return value of a function not being recognized in its call.

    This function clearly has its return value declared as complex*16:
    Code (Fortran):
    !----------4-vector dot product under Minkowski metric----------
    function dot4(v1,v2) result(res)
        implicit none
        complex*16 v1(4),v2(4)
        complex*16, dimension(0):: res
       
        res=v1(1)*v2(1)-v1(2)*v2(2)-v1(3)*v2(3)-v1(4)*v2(4)
        return
    end
    But then when I try to call dot4 from another function as such:
    Code (Fortran):
    !----------laser-dressed 4-vec----------
    function bar(v,k,mu) result(res)
        implicit none
        complex*16 v(4),k(4)
        complex*16, dimension(4):: res
        real*8 me,c, mu
       
        me=9.10938291E-31 !electron rest mass, in kg
        c=137.0 !speed of light, in au
       
        res=v+((mu*me*c)**2)/(4*dot4(k,v))*k
        return
    end
    it throws the following error on compiling (g95):
    I can't declare dot4 in the "bar" function because it would then be considered a local variable instead of a function being called. This creates the error of "dot4(k,v)" trying to look for a matrix element in dot4, causing a type mismatch within "bar".

    I was searching for information about this issue and saw that using modules and public statements may help, but I am confused as to how these work. Is this the right direction to be looking in, or is it some other syntax issue? Thanks.
     
  2. jcsd
  3. Jun 18, 2015 #2

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    In your program, the function DOT4 should be declared as REAL FUNCTION DOT4 (v1, v2). I would ignore the RESULT (res) and add a new last executable statement in the function routine which reads DOT4 = RES.

    In FUNCTION BAR, you can add a new declaration statement REAL DOT4. I would modify FUNCTION BAR in a similar manner as suggested for DOT4.

    For more information, see this article:

    http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/F90-Subprograms.pdf
     
  4. Jun 18, 2015 #3

    Svein

    User Avatar
    Science Advisor

    I do not like FORTRAN, but I think I can see it:

    function dot4(v1,v2) result(res)
    implicit none
    complex*16 v1(4),v2(4)
    complex*16 dot4 ← I think you must add this.
     
  5. Jun 18, 2015 #4
    The name of the function is just that, the function's name. There is no variable with this name used. I first tried it that way without using the result(res), but had no success. Making only that change you listed results in an error for trying to declare the function as a local variable.

    The issue with doing that with the function bar is that the returned value is a complex 4-vector. The declaration
    Code (Fortran):
    complex*16,  dimension(4) function bar...
    also throws a syntax error in the function definition.
     
  6. Jun 18, 2015 #5

    Svein

    User Avatar
    Science Advisor

  7. Jun 18, 2015 #6
    The double colon is just a formality as far as I know. Making that change,
    Code (Fortran):
    !----------4-vector dot product under Minkowski metric----------
    function dot4(v1,v2)! result(res)
        implicit none
        complex*16 v1(4),v2(4)
    !    complex*16, dimension(0):: res
        complex*16:: dot4
       
    !    res=v1(1)*v2(1)-v1(2)*v2(2)-v1(3)*v2(3)-v1(4)*v2(4)
        dot4=v1(1)*v2(1)-v1(2)*v2(2)-v1(3)*v2(3)-v1(4)*v2(4)
        return
    end
    results in the same error that was initially present.
     
  8. Jun 18, 2015 #7
  9. Jun 18, 2015 #8
    Thanks for trying to help, all. Putting all my my constants, functions, and subroutines into a "contains" block in a separate module file, and "use"ing that module in my main program file cleared up these errors. I have some really weird module related messages now though, so i'll make a new thread for that.
     
  10. Jun 18, 2015 #9
    avikatro:
    It would be best if you posted your entire program to get an idea of how it is structured...things can be in a single file and single program unit or could be over several files. The main program unit also allows for a 'contains' block, this fixes a lot of interface issues. Lastly, there is nothing wrong with function prototypes (function type declaration inside other subroutines/functions, Fortran also allows for that.

    gsal
     
  11. Jun 18, 2015 #10
    I got it worked out, thanks gsal. To anyone looking at this thread in the future taking a similar approach, the module errors were being generated by neglecting to include the module output file in my command line execution of my main program as such:

    Code (Text):
    g95 mainProg.f95 myModule.o -L/libs/dfftpack ...
    Note also that the module needs to be compiled separately as
    Code (Text):
    g95 myModule.f95 -c
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Fortran] Function implicit type error
  1. Fortran error (Replies: 33)

Loading...