24小时热门版块排行榜    

查看: 539  |  回复: 5
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

astringent

铜虫 (著名写手)


[交流] 【求助】子程序传值出错

我在主程序中调用了一个子程序test,然后再子程序中又调用了一个子程序test1.在子程序中的write语句都能得到ihbnum=1,可是主程序write语句的ihbnum去等于0,请大家帮忙看看,先谢了
program main
--
call test(a1,a2,----ihbnum1)
write (*,*) ihbnum1
enddo
子程序为:
subroutine test &
          (io,ih,ia,xxx,yyy,zzz,nattot,cell,atomname,resname,ihbnum)

      implicit none
      include 'constants.h'

      integer io,ih,ia,nattot,ihbnum
      double precision xxx(nattot),yyy(nattot),zzz(nattot)
      double precision cell(*)
      character*4 atomname(nattot)
      character*3 resname(nattot)

      integer iat

      ia = 0

      do iat=1, nattot
             if((resname(iat).eq.'DC5'.or.resname(iat).eq.'DC ').and.&
               (atomname(iat).eq.' N1 '.or.atomname(iat).eq.' N4 '.or.&
               atomname(iat).eq.' N3 '.or.atomname(iat).eq.' O2 ')then
               call test1 &
                   (io,ih,iat,xxx,yyy,zzz,nattot,cell,ihbnum)
               if(ihbnum.eq.1)  then
               ia=iat
               write(21,*) 'test',ihbnum,ia
             endif
      enddo
      
      return
      end

subroutine test1 &
          (io,ih,iat,xxx,yyy,zzz,nattot,cell,ihbnum)
-------
            ihbnum=0

      call bondcalculation(io,ih,xxx,yyy,zzz,nattot,cell,dx0,dy0,dz0,r0)
      call bondcalculation(io,iat,xxx,yyy,zzz,nattot,cell,dx1,dy1,dz1,r1)
           if (r1.lt.roamax) then
              theta = dacos(dx0*dx1+dy0*dy1+dz0*dz1)
                  if(theta.lt.thetamax*pi/180.0) then
                      call bondcalculation &
                         (ih,iat,xxx,yyy,zzz,nattot,cell,dx2,dy2,dz2,r2)
                        if (r2.lt.rhamax) then
                              ihbnum = 1
                        write(21,*) 'find',ihbnum,iat
                        endif
                   endif
           endif
                 
      return
      end
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

» 抢金币啦!回帖就可以得到:

查看全部散金贴

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

astringent

铜虫 (著名写手)


再问一下,对于子程序,这两个write语句输出的ihbnum的值是不是write(20)ihbnum=1,而write(21)的ihbnum=0?
subroutine test1 &
          (io,ih,iat,xxx,yyy,zzz,nattot,cell,ihbnum)
-------
            ihbnum=0

      call bondcalculation(io,ih,xxx,yyy,zzz,nattot,cell,dx0,dy0,dz0,r0)
      call bondcalculation(io,iat,xxx,yyy,zzz,nattot,cell,dx1,dy1,dz1,r1)
           if (r1.lt.roamax) then
              theta = dacos(dx0*dx1+dy0*dy1+dz0*dz1)
                  if(theta.lt.thetamax*pi/180.0) then
                      call bondcalculation &
                         (ih,iat,xxx,yyy,zzz,nattot,cell,dx2,dy2,dz2,r2)
                        if (r2.lt.rhamax) then
                              ihbnum = 1
                        write(20,*) 'find',ihbnum
                        endif
                   endif
           endif
          write(21,*) 'find',ihbnum     
      return
      end
3楼2010-11-23 06:32:40
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 6 个回答

ykwang

金虫 (正式写手)



astringent(金币+1):谢谢参与
astringent(金币+5): 2010-11-23 06:21:13
astringent(金币+5): 2010-11-29 15:55:59
主程序write语句的ihbnum等于0是因为子程序test在最后一步循环中调用test1时ihbnum的返回值为0。如果在子程序test中
   write(21,*) 'test',ihbnum,ia
加上下列条件返回语句
   IF(ihbnum.eq.1)THEN
        RETURN
    ENDIF
则主程序中write语句的ihbnum就会等于1。
2楼2010-11-23 06:06:52
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

ykwang

金虫 (正式写手)


从这段程序看,write(20,###)中写入的一定是ihbnum=1,但write(21,###)中写入的ihbnum除了ihbnum=0外还有ihbnum=1。
4楼2010-11-23 06:42:52
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

astringent

铜虫 (著名写手)


引用回帖:
Originally posted by ykwang at 2010-11-23 06:06:52:
主程序write语句的ihbnum等于0是因为子程序test在最后一步循环中调用test1时ihbnum的返回值为0。如果在子程序test中
   write(21,*) 'test',ihbnum,ia
...

i have tried use the return, but it does not work, how can i solve this?
6楼2010-11-23 19:20:11
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见