Fortran中的模块怎么定义和使用

2024-04-13

在Fortran中,模块可以用来封装变量、常量、子程序和函数,以便在程序中重复使用。下面是一个简单的示例来展示如何定义和使用一个模块:

module my_module
  implicit none
  integer :: my_variable

contains

  subroutine my_subroutine()
    print *, "Hello from my subroutine!"
  end subroutine my_subroutine

end module my_module

program main_program
  use my_module
  implicit none
  
  ! 定义变量并赋值
  my_variable = 10
  
  ! 调用模块中的子程序
  call my_subroutine()
  
end program main_program

在这个示例中,首先定义了一个名为my_module的模块,里面包含了一个整型变量my_variable和一个子程序my_subroutine。在主程序中,通过use my_module语句引入了模块,然后可以直接使用模块中定义的变量和子程序。

通过模块的使用,可以将代码模块化,提高代码的可读性和维护性。

《Fortran中的模块怎么定义和使用.doc》

下载本文的Word格式文档,以方便收藏与打印。