Community
cancel
Showing results for
Did you mean:
Highlighted
Beginner
15 Views

## Function return value not what is expected (F77)

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.

Tags (1)

Accepted Solutions
Valued Contributor III
15 Views

## Try the code below. It is

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```

2 Replies
Valued Contributor III
16 Views

## Try the code below. It is

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```

Highlighted
Beginner
15 Views