I just wanted to understand how functions, modules, .. work. So I played around a little and I apparently make I mistake when I want to allocate a matrix:
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
Now, if I want to allocate the matrix within the function 'testits', the program stops. But if I want to allocate the matrix B in the program, it works. I don't get why it works within the program but not within the function. I declared the variables and all the necessary stuff in the same way, so why should there be any difference?