Fortran [Fortran] Function implicit type error

AI Thread Summary
The discussion revolves around a compilation error in Fortran related to the return type of a function not being recognized when called from another function. The function `dot4`, intended to compute a 4-vector dot product, is declared with a return type of `complex*16`, but the compiler throws an error indicating that it has no implicit type when called from the `bar` function. Suggestions include modifying the return type declaration of `dot4` and ensuring that it is properly recognized in the `bar` function. The use of modules and public statements is mentioned as a potential solution to manage function visibility and types, leading to a more organized structure. Ultimately, the user resolved the issue by placing all functions and constants into a module and using it in the main program, emphasizing the importance of proper compilation commands for modules. The discussion highlights common pitfalls in Fortran regarding function declarations and module management.
avikarto
Messages
56
Reaction score
9
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:
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:
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):
res=v+((mu*me*c)**2)/(4*dot4(k,v))*k
.........1
Error: function 'dot4' at (1) has no implicit type
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.
 
Technology news on Phys.org
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
 
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.
 
Svein said:
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.

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.

SteamKing said:
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

The issue with doing that with the function bar is that the returned value is a complex 4-vector. The declaration
Fortran:
complex*16,  dimension(4) function bar...
also throws a syntax error in the function definition.
 
Svein said:
As I said, I do not like FORTRAN. I used Google (https://en.wikibooks.org/wiki/Fortran/Fortran_procedures_and_functions) and found that I had missed a double colon.

function dot4(v1,v2) result(res)
implicit none
complex*16 v1(4),v2(4)
complex*16 :: dot4 ← I think you must add this.

The double colon is just a formality as far as I know. Making that change,
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.
 
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.
 
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
 
  • #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:
g95 mainProg.f95 myModule.o -L/libs/dfftpack ...
Note also that the module needs to be compiled separately as
Code:
g95 myModule.f95 -c
 

Similar threads

Replies
4
Views
2K
Replies
8
Views
4K
Replies
16
Views
2K
Replies
8
Views
2K
Replies
7
Views
3K
Replies
5
Views
15K
Back
Top