Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
53 Views

Function return value not what is expected (F77)

Jump to solution

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

Accepted Solutions
Highlighted
Valued Contributor III
53 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
Highlighted
Valued Contributor III
54 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
Highlighted
Beginner
53 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