// An highlighted block
!module 不是函数,是封装模块,用来把具有一定功能的子例程subroutine或者子程序function,
!或者变量封装起来,便于后面的直接调用,如果把子例程subroutine用module封装起来,后面在主程
!序中调用子例程,便可以省略显示接口interface,但是本文主要讲如何调用module中的function函数
module date
contains
function plus(a,b) result(ans)
real::ans
real::a,b
ans=a+b
end function plus
end module date
!首先用module封装了一个函数,注意,在module中定义子例程或者子程序时,必须写contains,然后换行开始写函数
!在这儿自定义函数plus,并且使用了resul函数,用ans在子程序中接收输出结果,
!因此在子程序中需要声明 ans的类型,不必声明function name的数据类型
program main
use date
implicit none
real::a1,b1
parameter(a1=1.0,b1=2.3)
print*,plus(a1,b1)
end program main
!在主程序中需要调用模块里面的内容,方法 use+模块名
!调用plus函数,原本在调用函数时需要声明调用函数的数据类型,但是我们这儿调用了date 模块,由于我们在模块中已经定义了函数结果ans的数据类型,因此在主程序中不需要再定义plus的数据类型。
比如在主程序中定义plus的数据类型
program main
use date
implicit none
real,external::plus
real::a1,b1
parameter(a1=1.0,b1=2.3)
print*,plus(a1,b1)
end program main
!报错:错误 1 error #6401: The attributes of this name conflict with those made accessible by a USE statement.
Fortran之如何调用封装模块里面的function函数
最新推荐文章于 2024-05-09 23:36:34 发布