Fortran GCC interface compiler bug?
-
01-06-2021 - |
Question
Why does GCC not warn me when I forget to specify the return type of a interface function? For me this behavior seams unexpected. What do you say to this?
This is the test-program (uncoment Line 6 and it works as it should):
program returntest
implicit none
interface
function givehalf(Y)
double precision :: Y
!double precision :: givehalf !<-- Uncomment this line
end function givehalf
end interface
double precision :: temp
temp=givehalf(5.151515d0)
print*, 'result= ',temp
end program returntest
function givehalf(Y)
implicit none
double precision :: Y
double precision :: givehalf
print*, 'Y= ',Y
givehalf=Y/2.0d0
print*, 'return Y/2',givehalf
return
end function givehalf
The result is this:
user@bapf028dl:/media/disk> gfortran44 -Wall return-test.f90
user@bapf028dl:/media/disk> ./a.out
Y= 5.1515149999999998
return Y/2 2.5757574999999999
result= -1.0579199790954590
user@bapf028dl:/media/disk> ifort return-test.f90
user@bapf028dl:/media/disk> ./a.out
Y= 5.15151500000000
return Y/2 2.57575750000000
result= 2.57575750350952
Solution
This is not a bug. The interface body inside an interface block forms a separate scope, so you should include an implicit none
statement there to prevent yourself from making such errors. Without it the implicit typing rules are in effect, so the function is expected to return a real
.
interface
function givehalf(Y)
implicit none !<-- now you should get an error during compilation
double precision :: Y
!double precision :: givehalf !<-- Uncomment this line
end function givehalf
end interface
OTHER TIPS
edit: It is really a bug. It gives a type error in gfortran 4.6 and 4.7.
Also I would recommend you to use a module for your functions. You have only one place to change.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow