[Fortran] Function implicit type error

Click For Summary

Discussion Overview

The discussion revolves around a Fortran programming issue related to function type errors, specifically concerning the return type of a function and its recognition during calls. Participants explore potential syntax issues, declarations, and the use of modules to resolve these errors.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant describes an issue with the return value of the function 'dot4' not being recognized, leading to a compilation error.
  • Another participant suggests declaring 'dot4' as a REAL FUNCTION and modifying the return statement to assign the result directly to 'DOT4'.
  • Some participants propose adding a declaration for 'dot4' within the function to resolve the implicit type error.
  • There are mentions of needing to use a double colon for variable declarations, with some participants expressing uncertainty about its necessity.
  • A participant shares that moving functions and constants into a module and using a 'contains' block resolved some of their errors, although new module-related issues arose.
  • Another participant suggests that posting the entire program could help clarify the structure and resolve interface issues.
  • A later reply indicates that neglecting to include the module output file in the command line execution caused additional errors, highlighting the importance of proper compilation steps.

Areas of Agreement / Disagreement

Participants express various opinions on how to resolve the function type error, with no consensus reached on a single solution. Different approaches are suggested, and some participants indicate confusion about the syntax and structure of Fortran functions.

Contextual Notes

Some limitations in the discussion include unresolved syntax issues, the need for clarity on the use of modules, and the dependence on specific compiler behaviors. There are also references to potential errors arising from the structure of the program and compilation commands.

Who May Find This Useful

This discussion may be useful for Fortran programmers encountering similar function type errors, particularly those dealing with complex data types and module usage.

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
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 5 ·
Replies
5
Views
16K