24小时热门版块排行榜    

查看: 1341  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 344求调剂 +3 desto 2026-03-24 3/150 2026-03-24 10:09 by 搏击518
[考研] 298-一志愿中国农业大学-求调剂 +11 手机用户 2026-03-17 12/600 2026-03-23 23:51 by 热情沙漠
[考研] 求材料,环境专业调剂 +3 18567500178 2026-03-18 3/150 2026-03-23 23:50 by 热情沙漠
[考研] 环境学硕288求调剂 +8 皮皮皮123456 2026-03-22 8/400 2026-03-23 23:47 by 热情沙漠
[考研] 材料292调剂 +4 橘颂思美人 2026-03-23 4/200 2026-03-23 23:16 by peike
[考研] 384求调剂 +3 子系博 2026-03-22 6/300 2026-03-23 21:45 by 子系博
[考研] 336求调剂 +4 收到VS 2026-03-20 4/200 2026-03-23 19:02 by macy2011
[考研] 298求调剂 +8 上岸6666@ 2026-03-20 8/400 2026-03-23 11:02 by laoshidan
[考研] 307求调剂 +11 冷笙123 2026-03-17 11/550 2026-03-22 20:16 by edmund7
[考研] 280求调剂 +11 咕噜晓晓 2026-03-18 12/600 2026-03-21 22:40 by ACS Nano——
[考研] 化学调剂 +5 yzysaa 2026-03-21 5/250 2026-03-21 22:12 by peike
[考研] 材料与化工(0856)304求B区调剂 +3 邱gl 2026-03-20 7/350 2026-03-21 19:05 by 15709483992
[考研] 一志愿重庆大学085700资源与环境总分308求调剂 +7 墨墨漠 2026-03-20 7/350 2026-03-21 16:36 by barlinike
[考研] 材料与化工(0856)304求 B区 调剂 +3 邱gl 2026-03-21 3/150 2026-03-21 13:47 by lature00
[考研] 求调剂 +3 白QF 2026-03-21 3/150 2026-03-21 13:12 by zhukairuo
[考研] 求调剂 +6 Mqqqqqq 2026-03-19 6/300 2026-03-21 08:04 by JourneyLucky
[考研] 22408 344分 求调剂 一志愿 华电计算机技术 +4 solanXXX 2026-03-20 4/200 2026-03-20 23:49 by alg094825
[考研] 308求调剂 +3 阿姐阿姐家啊 2026-03-18 3/150 2026-03-20 23:24 by JourneyLucky
[考研] 321求调剂 +9 何润采123 2026-03-18 11/550 2026-03-20 23:19 by JourneyLucky
[考研] 0817 化学工程 299分求调剂 有科研经历 有二区文章 +22 rare12345 2026-03-18 22/1100 2026-03-20 20:39 by zhukairuo
信息提示
请填处理意见