caimaxwell 发表于 2023-3-7 14:55:06

关于module及subroutine

module EmiblockData
use EmHMatrixData
implicit none
type Subblocks
    integer*4:: row
    integer*4:: coL
end type Subblocks

type blocks
    integer*4:: row1,row2,coL1,coL2
    integer*4:: rowm,coLm
    integer*4:: dad
    integer*4:: son(4)
    integer*4:: bnum
    type(Subblocks), allocatable :: iSubblocks(:)
end type blocks

integer*4 :: Nblock
type(blocks), allocatable :: iblocks(:)
integer*4, allocatable   :: BlockInd(:, :)
!type(Subblocks), allocatable:: iSubblocks(:)
integer*4, allocatable:: iSubblocks(:), iblockInd(:, :)

contains
subroutine BlockRenumber(iSubblocks, sbmax)
!对子矩阵块中的矩阵块进行编号
implicit none
integer*4:: sbmax               
type(Subblocks),allocatable:: iSubblocks(:)      !最小矩阵块....
end subroutine
为什么我明明已经在module里声明了isubblocks,在编写子程序时仍然要声明isubblock(),不声明就会报错(未定义)?

望解答

楚香饭 发表于 2023-3-7 15:33:07

因为虚参会覆盖module中的模块变量。

caimaxwell 发表于 2023-3-7 15:42:15

是subroutine(x,y,z)中的x,y,z覆盖了module中声明的变量吗?就是说subroutine()括号中不能出现module声明的变量,应使用别的单词代替的意思吗?

caimaxwell 发表于 2023-3-7 19:57:19

十分感谢您的解答。很有帮助!

楚香饭 发表于 2023-3-7 20:08:23

本帖最后由 楚香饭 于 2023-3-7 20:11 编辑

是的。
subroutine BlockRenumber(iSubblocks, sbmax)
这里的 iSubblocks 覆盖了 module 中定义的 iSubblocks

并不是说:“subroutine()括号中不能出现module声明的变量”
而是一旦出现了,那么 module 中定义的同名变量,在这个subroutine中就无法访问了。
这种情况,称为“覆盖”
如果你确实认为虚参中的那个变量才是你想要的,module中同名的变量你并不希望使用。那么这样做,也并无不可。
实际上,某些情况下,这样恰好是你想要的。
页: [1]
查看完整版本: 关于module及subroutine