Segfault when calling a function with a constant argument

前端 未结 5 454
一个人的身影
一个人的身影 2020-12-21 13:58

I have written this very simple code in Fortran:

program su
  implicit none
  real ran3
  write(*,*) ran3(0)
end program su

real*8 function ran3(iseed)
  im         


        
5条回答
  •  旧时难觅i
    2020-12-21 14:36

    I see two problems with the code. The first is the one which I think is the cause of the error. The function ran3 is referenced with the constant 0 as the actual argument, but the corresponding dummy argument iseed is used on the left side of an assignment statement in the function. This is an error: you can't change the value of zero.

    The second error is that ran3 returns a real*8 (whatever that may be; it's a non-standard declaration), but in the main program ran3 is declared as being a default real.

    The following program and function compile with gfortran 4.7.2.

    program su
        implicit none
        real :: ran3
    
        write(*, *) ran3(0)
    end program su
    
    function ran3(iseed)
        implicit none
        integer :: iseed, temp
        real :: ran3
    
        temp = iseed * 153941 + 1
        ran3 = temp * 2.328 + 0.5
    end function ran3
    

提交回复
热议问题