Search code examples
functionfortrangfortrantype-mismatch

Fortran 77 function return type mismatch


I've been trying to learn Fortran 77 for a university project and I write the following code in windows 10 :

program Q_Value
real BEs(4),Masses(4)
real Q,Tthr
integer i,Nuclei(4,2),A,Z

do 10 i=1,4,1
        write(*,100)'Give the info for Nuclei:',i
 100        format(A,I1)
        write(*,*)'A='
        read(*,*)Nuclei(i,1)
        write(*,*)'Z='
        read(*,*)Nuclei(i,2)
        if((Nuclei(i,1).EQ.0).OR.(Nuclei(i,2).EQ.0))then
            BEs(i)=0.
        else
            BEs(i)=BetheWeiss(Nuclei(i,1),Nuclei(i,2))
        endif
        Masses(i)=Mass(Nuclei(i,1),Nuclei(i,2),BEs(i))
 10 continue

[...]
end
real function Mass(A,Z,BE)
    integer A,Z
    real BE,mass
c   local var's
    parameter(Mp=938.2720813,Mn=939.5654133)
c   statements
    Mass=((A-Z)*Mn)+(Z*Mp)-BE
    return 
    end

When compiling in GNU Fortran (gfortran 8.1.0) I get the following error:

Masses(i)=Mass(Nuclei(i,1),Nuclei(i,2),BEs(i))
                     1
Error: Return type mismatch of function 'mass' at (1) (INTEGER(4)/REAL(4))

Can anyone help me out with this because as far as I am concerned my function return a real number and Masses(i) is a real variable.


Solution

  • The default implicit typing rule is that if the first letter of the name is I , J , K , L , M , or N , then the data type is integer, otherwise it is real.

    Because your function is mass(), it starts with M and is not declared within the main program, it is considered to be an integer function there. That conflicts with the function declaration and the compiler complains. What you need, if you want to keep the extremely ancient FORTRAN 77 is

    real mass
    

    in the main program.

    It is much better to use the features of modern Fortran, starting with IMPLICIT NONE and putting the function into a module or making it internal to the main program.

    You could also fix the error message using an alternative implicit statement in the main program (see my comment) but I strongly discourage anything else than IMPLICIT NONE.