[Fortran] Function implicit type error

In summary, to fix the issue with the declared type of the return value of a function not being recognized in its call, the function should be declared as REAL FUNCTION DOT4 (v1, v2) and the RESULT (res) declaration can be ignored. Additionally, if using modules, make sure to include the module output file in the command line execution of the main program and compile the module separately before using it in the main program.
  • #1
avikarto
56
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
  • #2
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
 
  • #3
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.
 
  • #4
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.
 
  • #6
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.
 
  • #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.
 
  • #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
 
  • #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
 

Related to [Fortran] Function implicit type error

1. What is a function implicit type error in Fortran?

A function implicit type error in Fortran occurs when the data types of a function's input parameters do not match the data types specified in the function definition. This can lead to unexpected results or errors when the function is called.

2. How can I prevent function implicit type errors in Fortran?

To prevent function implicit type errors in Fortran, it is important to carefully specify the data types of all input parameters in the function definition and ensure that the data types of the inputs when the function is called match those specified in the definition.

3. What causes function implicit type errors in Fortran?

Function implicit type errors in Fortran can be caused by a variety of factors, including incorrect data type declarations, mismatched data types between the function definition and the function call, or using the wrong data types for a specific operation within the function.

4. How are function implicit type errors detected in Fortran?

Function implicit type errors in Fortran are typically detected by the compiler during the compilation process. The compiler will flag any lines of code where data types do not match, allowing the programmer to identify and fix the error before running the program.

5. Are there any common techniques for debugging function implicit type errors in Fortran?

One common technique for debugging function implicit type errors in Fortran is to use the "implicit none" statement at the beginning of the program, which forces all variables to be explicitly declared. This can help catch any implicit type errors that may otherwise go unnoticed. Additionally, using print statements or a debugger can help identify the source of the error.

Similar threads

  • Programming and Computer Science
Replies
6
Views
979
  • Programming and Computer Science
Replies
4
Views
741
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
396
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
2
Views
813
  • Programming and Computer Science
Replies
16
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top