24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 1343  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 352分 化工与材料 +3 海纳百川Ly 2026-03-27 3/150 2026-03-27 17:18 by 杨杨杨紫
[考研] 085602 307分 求调剂 +3 不知道叫什么! 2026-03-26 3/150 2026-03-27 16:50 by laoshidan
[考研] 279 分 求调剂 +4 睡个好觉_16 2026-03-24 4/200 2026-03-27 15:05 by 醉在风里
[考研] 08开头275求调剂 +4 拉谁不重要 2026-03-26 4/200 2026-03-27 14:12 by Delta2012
[考研] 化学308分求调剂 +8 你好明天你好 2026-03-23 9/450 2026-03-27 14:01 by 杨光于青云
[考研] 311求调剂 +7 lin0039 2026-03-26 7/350 2026-03-27 12:42 by 果果妈咪
[考研] 0703化学/290求调剂/本科经历丰富/工科也可 +7 丹青奶盖 2026-03-26 8/400 2026-03-27 11:24 by 丹青奶盖
[考研] 314求调剂 +3 溪云珂 2026-03-26 3/150 2026-03-27 11:20 by sanrepian
[考研] 286求调剂 +4 lim0922 2026-03-26 4/200 2026-03-27 10:28 by guoweigw
[考研] 304求调剂 +3 曼殊2266 2026-03-27 3/150 2026-03-27 10:17 by guoweigw
[考研] 349求调剂 +4 李木子啊哈哈 2026-03-25 4/200 2026-03-26 22:49 by fmesaito
[考研] 325求调剂 +3 Aoyijiang 2026-03-23 3/150 2026-03-26 20:46 by 不吃魚的貓
[考研] 材料调剂 5+4 想要一壶桃花水 2026-03-25 10/500 2026-03-26 19:56 by 不吃魚的貓
[考研] 一志愿 南京邮电大学 288分 材料考研 求调剂 +3 jl0720 2026-03-26 3/150 2026-03-26 13:39 by zzll406
[考研] 材料与化工328分调剂 +6 。,。,。,。i 2026-03-23 6/300 2026-03-25 22:30 by 418490947
[考研] 085404电子信息284分求调剂 +4 13659058978 2026-03-24 4/200 2026-03-24 12:15 by syl20081243
[考研] 求老师收我 +3 zzh16938784 2026-03-23 3/150 2026-03-23 12:56 by ztnimte
[考研] 280分求调剂 一志愿085802 +4 PUMPT 2026-03-22 7/350 2026-03-22 22:13 by 星空星月
[考研] 求调剂 +4 要好好无聊 2026-03-21 4/200 2026-03-21 18:57 by 学员8dgXkO
[考研] 0805材料320求调剂 +3 深海物语 2026-03-20 3/150 2026-03-21 15:46 by 无际的草原
信息提示
请填处理意见