我只想了解函数,模块,...的工作原理。所以我玩了一下,当我想分配一个矩阵时,我显然错了:
Module test
implicit none
real, allocatable, dimension(:,:) :: A
End module test
Program Functiontest
use test
implicit none
real :: testits
real, allocatable, dimension(:,:) :: Ausgabe
integer ::l=1, ierror=0
real, allocatable, dimension(:,:) :: B
Ausgabe=testits(l)
print *, 'ausgabe= ', Ausgabe
if (.not. allocated(B)) then
allocate(B(3,5), STAT=ierror)
print *, 'allocate 2'
end if
if (ierror.neqv.0) then
print *, 'does not work either', B
stop
end if
End program functiontest
Function testits(l) result(B)
Use test
implicit none
integer, intent(in) :: l
integer :: ierror=0
real, allocatable, dimension(:,:) :: B
allocate(A(3,5))
A=0.0e0
!if (.not. allocated(B)) then
! allocate(B(3,5), STAT=ierror)
! print *, 'allocate'
! end if
! if (ierror.neqv.0) then
! print *, 'does not work'
! stop
! end if
B=A
End function testits
现在,如果我想在函数'testits'中分配矩阵,程序就会停止。但是如果我想在程序中分配矩阵B,它就可以工作。我不知道为什么它在程序中工作但不在函数内。我以同样的方式宣布了变量和所有必要的东西,那为什么会有任何区别呢?