24小时热门版块排行榜    

查看: 1308  |  回复: 0

drunkfsoul

铁虫 (初入文坛)

[求助] Fortran语言:自定函数使用allocatable变量返回的问题

运行程序,result(dat)返回的结果是allocate(dat(2))后dat初始化的值,跟    dat(1)=temp,  dat(2)=1的运算没有关系。
程序监控显示,调用function gcd(a,b)时,在end function之前,dat的结果还是对的。
请问,为什么返回时,dat的值会变成初始状态呢?
我只确定跟allocateable有关,具体原因还是没搞明白。请各位大神指教!

下面是我的程序:
module gcd_module !测试可变变量返回问题,原程序为求取最小公约数。
contains
  function gcd(a,b) result(dat)
  implicit none
  integer, intent (in) :: a, b
  integer, allocatable :: dat(: )
  integer :: temp

    if (a<b) then
      temp = a
    else
      temp = b
    end if
    do while ((mod(a,temp)/=0) .or. (mod(b,temp)/=0))
      temp = temp - 1
    end do

  allocate(dat(2))
  dat(1)=temp
  dat(2)=1
  end function gcd
end module gcd_module

program ch1207
use gcd_module
implicit none
integer :: i, j
integer,dimension(2):: result

  print *, ' type in two integers'
  read *, i, j
  result = gcd(i,j)
  print *, ' gcd is ', result
end program ch1207
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

相关版块跳转 我要订阅楼主 drunkfsoul 的主题更新
信息提示
请填处理意见