在Fortran中重構和模塊化代碼可以通過以下步驟實現:
module
關鍵字來定義一個模塊。例如:module mymodule
implicit none
private
contains
subroutine sub1()
! sub1 code
end subroutine
end module
use
語句來引用已經定義的模塊,以便使用其中定義的子程序和變量。例如:program main
use mymodule
implicit none
call sub1()
end program
module mymodule
implicit none
contains
subroutine sub1()
! sub1 code
end subroutine
end module
program main
use mymodule
implicit none
interface
subroutine sub1()
end subroutine
end interface
call sub1()
end program
type
來實現。例如:module mymodule
implicit none
type :: mytype
integer :: data
end type
contains
subroutine sub1(obj)
type(mytype), intent(inout) :: obj
! sub1 code
end subroutine
end module