爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4166|回复: 4

fortran中module的使用问题

[复制链接]
发表于 2015-1-7 17:06:54 | 显示全部楼层 |阅读模式
Fortran
系统平台: cvf6.6
问题概况: use module问题
问题截图: -
我看过提问的智慧: 看过
自己思考时长(天): 1

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
module ma
    implicit none
    integer a,b
contains
   subroutine getx(a,b)
     implicit none
     write(*,*) a+b
   end subroutine
end module

program main
    use ma
    implicit none
    call getx(1,2)
end

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
module ma
    implicit none
    integer a,b
contains
   subroutine getx()
     implicit none
     write(*,*) a+b
   end subroutine
end module

program main
    use ma
    implicit none
    a=1
    b=2
    call getx()
end

!!!!!!!!!!!!!!!!!!
请问谁能解答一下,为什么按照第一种写法会出错,而第二种写法就不会报错呢

密码修改失败请联系微信:mofangbao
发表于 2015-1-7 18:31:54 | 显示全部楼层
你的第一个程序中
subroutine getx(a,b)
     implicit none
     write(*,*) a+b
   end subroutine
a和b出现在dummy argument列表中。但是你在subroutine中并没有对其进行声明,
所以出错。
而且要注意,在其他用到已经定义的module的program unit(main program,subroutine, function, module 等) 中声明的变量尽量不要与module中已经声明的变量重名。
密码修改失败请联系微信:mofangbao
 楼主| 发表于 2015-1-7 19:50:52 | 显示全部楼层
ningking1986 发表于 2015-1-7 18:31
你的第一个程序中
subroutine getx(a,b)
     implicit none

您好,请问subroutine getx(a,b)括号里出现的a,b不就是module中声明的a,b吗?dummy agrument指的是subroutine getx(a,b)里用于接收参数的a,b,还是subroutine里声明的所有参数啊?
密码修改失败请联系微信:mofangbao
发表于 2015-1-7 20:26:51 | 显示全部楼层
schLltt 发表于 2015-1-7 19:50
您好,请问subroutine getx(a,b)括号里出现的a,b不就是module中声明的a,b吗?dummy agrument指的是subrou ...

subroutine getx(a,b) 中的a,b 与你module中声明的a,b没有任何关系,他们只是虚参数(dummy argument)用来接收传给子程序的实参的指针。
module ma
    implicit none
    integer a,b
contains

subroutine getx(a,b)
     implicit none
     integer,intent(in):: a
     integer,intent(in):: b
     write(*,*) a+b
   end subroutine
end module

program main
    use ma
    implicit none
   
    call getx(1,2)
end

这样应该能行。不过你在module中声明的a,b还没有进行初始化。
密码修改失败请联系微信:mofangbao
 楼主| 发表于 2015-1-7 21:28:50 | 显示全部楼层
ningking1986 发表于 2015-1-7 20:26
subroutine getx(a,b) 中的a,b 与你module中声明的a,b没有任何关系,他们只是虚参数(dummy argument)用 ...

非常感谢!说的很清楚呢!
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表