Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28434 Discussions

Function return value not what is expected (F77)

Jonas_T_2
Beginner
519 Views

Hi,

I am having a hard time understanding the behaviour of some FORTRAN 77 code.

The code:

      INTEGER*4 FUNCTION EINAUS(IY,IK)
      INTEGER*4 IRE
      IRE = 0
      EINAUS = IRE
      END FUNCTION
      
      SUBROUTINE STDAUS
      INTEGER*4 IRE
      IRE = 1
      WRITE(*,*) 'IRE= ',IRE
      WRITE(*,*) 'EINAUS(1,3)= ',EINAUS(1,3)
      IRE = EINAUS(1,3)
      WRITE(*,*) 'IRE= ',IRE
      RETURN
      END

The output:

 IRE=            1
 EINAUS(1,3)=             NaN
 IRE=  -2147483648

I would have expected the last two lines to read 0 instead of NaN or 0x80000000.

Please point me to the obvious thing I am missing here.

0 Kudos
1 Solution
andrew_4619
Honored Contributor II
519 Views

Try the code below. It is however much better to have your functions in a module rather than external.

 

      SUBROUTINE STDAUS
      INTEGER*4 IRE
      integer(4), external :: EINAUS ! declare the function
      IRE = 1
      WRITE(*,*) 'IRE= ',IRE
      WRITE(*,*) 'EINAUS(1,3)= ',EINAUS(1,3)
      IRE = EINAUS(1,3)
      WRITE(*,*) 'IRE= ',IRE
      RETURN
      END

 

View solution in original post

0 Kudos
2 Replies
andrew_4619
Honored Contributor II
520 Views

Try the code below. It is however much better to have your functions in a module rather than external.

 

      SUBROUTINE STDAUS
      INTEGER*4 IRE
      integer(4), external :: EINAUS ! declare the function
      IRE = 1
      WRITE(*,*) 'IRE= ',IRE
      WRITE(*,*) 'EINAUS(1,3)= ',EINAUS(1,3)
      IRE = EINAUS(1,3)
      WRITE(*,*) 'IRE= ',IRE
      RETURN
      END

 

0 Kudos
Jonas_T_2
Beginner
519 Views

Thank you very much. That's it.

I keep all code, that I added, in modules. But since it is not mine, and I just had to make some adjustments (procedure to function) I'll add it in like that. I'll try to convince him, since it would reduce that huge amount of COMMON blocks aswell.

0 Kudos
Reply