- Fortran 教程
- Fortran - 首页
- Fortran - 概述
- Fortran - 环境设置
- Fortran - 基本语法
- Fortran - 数据类型
- Fortran - 变量
- Fortran - 常量
- Fortran - 运算符
- Fortran - 决策
- Fortran - 循环
- Fortran - 数字
- Fortran - 字符
- Fortran - 字符串
- Fortran - 数组
- Fortran - 动态数组
- Fortran - 派生数据类型
- Fortran - 指针
- Fortran - 基本输入输出
- Fortran - 文件输入输出
- Fortran - 过程
- Fortran - 模块
- Fortran - 内在函数
- Fortran - 数值精度
- Fortran - 程序库
- Fortran - 编程风格
- Fortran - 程序调试
- Fortran 资源
- Fortran - 快速指南
- Fortran - 有用资源
- Fortran - 讨论
Fortran - 模块
模块就像一个包,您可以将函数和子程序放在其中,如果您正在编写一个非常大的程序,或者您的函数或子程序可以在多个程序中使用。
模块为您提供了一种在多个文件之间分割程序的方法。
模块用于:
打包子程序、数据和接口块。
定义可被多个例程使用的全局数据。
声明可以在您选择的任何例程中使用的变量。
导入整个模块以在另一个程序或子程序中使用。
模块语法
模块由两部分组成:
- 语句声明的规范部分
- 包含子程序和函数定义的包含部分
模块的一般形式为:
module name [statement declarations] [contains [subroutine and function definitions] ] end module [name]
在程序中使用模块
您可以通过 use 语句将模块合并到程序或子程序中:
use name
请注意
您可以根据需要添加任意数量的模块,每个模块都将放在单独的文件中并单独编译。
一个模块可以在各种不同的程序中使用。
同一个程序可以多次使用同一个模块。
在模块规范部分声明的变量对于该模块是全局的。
在模块中声明的变量在使用该模块的任何程序或例程中都成为全局变量。
use 语句可以出现在主程序或任何其他使用在特定模块中声明的例程或变量的子程序或模块中。
示例
以下示例演示了该概念:
module constants
implicit none
real, parameter :: pi = 3.1415926536
real, parameter :: e = 2.7182818285
contains
subroutine show_consts()
print*, "Pi = ", pi
print*, "e = ", e
end subroutine show_consts
end module constants
program module_example
use constants
implicit none
real :: x, ePowerx, area, radius
x = 2.0
radius = 7.0
ePowerx = e ** x
area = pi * radius**2
call show_consts()
print*, "e raised to the power of 2.0 = ", ePowerx
print*, "Area of a circle with radius 7.0 = ", area
end program module_example
编译并执行上述程序后,将产生以下结果:
Pi = 3.14159274 e = 2.71828175 e raised to the power of 2.0 = 7.38905573 Area of a circle with radius 7.0 = 153.938049
模块中变量和子程序的可访问性
默认情况下,模块中的所有变量和子程序都通过use语句提供给使用模块代码的程序。
但是,您可以使用private和public属性来控制模块代码的可访问性。当您将某些变量或子程序声明为private时,它在模块外部不可用。
示例
以下示例说明了这个概念:
在上一个示例中,我们有两个模块变量,e和pi。让我们将它们设为private并观察输出:
module constants
implicit none
real, parameter,private :: pi = 3.1415926536
real, parameter, private :: e = 2.7182818285
contains
subroutine show_consts()
print*, "Pi = ", pi
print*, "e = ", e
end subroutine show_consts
end module constants
program module_example
use constants
implicit none
real :: x, ePowerx, area, radius
x = 2.0
radius = 7.0
ePowerx = e ** x
area = pi * radius**2
call show_consts()
print*, "e raised to the power of 2.0 = ", ePowerx
print*, "Area of a circle with radius 7.0 = ", area
end program module_example
编译并执行上述程序后,将显示以下错误消息:
ePowerx = e ** x 1 Error: Symbol 'e' at (1) has no IMPLICIT type main.f95:19.13: area = pi * radius**2 1 Error: Symbol 'pi' at (1) has no IMPLICIT type
由于e和pi都被声明为private,程序module_example无法再访问这些变量。
但是,其他模块子程序可以访问它们:
module constants
implicit none
real, parameter,private :: pi = 3.1415926536
real, parameter, private :: e = 2.7182818285
contains
subroutine show_consts()
print*, "Pi = ", pi
print*, "e = ", e
end subroutine show_consts
function ePowerx(x)result(ePx)
implicit none
real::x
real::ePx
ePx = e ** x
end function ePowerx
function areaCircle(r)result(a)
implicit none
real::r
real::a
a = pi * r**2
end function areaCircle
end module constants
program module_example
use constants
implicit none
call show_consts()
Print*, "e raised to the power of 2.0 = ", ePowerx(2.0)
print*, "Area of a circle with radius 7.0 = ", areaCircle(7.0)
end program module_example
编译并执行上述程序后,将产生以下结果:
Pi = 3.14159274 e = 2.71828175 e raised to the power of 2.0 = 7.38905573 Area of a circle with radius 7.0 = 153.938049
广告