在Fortran中重構(gòu)和模塊化代碼可以通過以下步驟實現(xiàn):
module
關(guān)鍵字來定義一個模塊。例如:module mymodule
implicit none
private
contains
subroutine sub1()
! sub1 code
end subroutine
end module
use
語句來引用已經(jīng)定義的模塊,以便使用其中定義的子程序和變量。例如: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
來實現(xiàn)。例如: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