How can I introduce an allocatable array which allocate in subroutine?

Hi @VOLCANIC_9, further to @Beliavsky’s explanation of modern compilers, be aware that functions and subroutines that aren’t placed in modules have limited functionality; in this case you cannot pass unallocated allocatable arrays to subroutines for allocation unless they are in a module or you specify the interface yourself.

The following two solutions should work with a modern Fortran compiler:

Subroutine in module (click to expand)
module routines
implicit none

contains

subroutine sub(A,B,C)

REAL, DIMENSION(:,:), allocatable, intent(out) :: A
REAL, DIMENSION(:,:), allocatable, intent(out) :: B
REAL, DIMENSION(:,:), allocatable, intent(out) :: C

allocate(A(10,10))
allocate(B(20,20))
allocate(C(30,30))

end subroutine sub

end module routines

program PROG
use routines
implicit none

REAL, DIMENSION(:,:), ALLOCATABLE :: A
REAL, DIMENSION(:,:), ALLOCATABLE :: B
REAL, DIMENSION(:,:), ALLOCATABLE :: C

CALL SUB(A,B,C)

write(*,*) size(A,1), ',', size(A,2)
write(*,*) size(B,1), ',', size(B,2)
write(*,*) size(C,1), ',', size(C,2)

END program PROG
Explicit subroutine interface (click to expand)
program PROG

implicit none

interface
  subroutine sub(A,B,C)
        REAL, DIMENSION(:,:), allocatable, intent(out) :: A
        REAL, DIMENSION(:,:), allocatable, intent(out) :: B
        REAL, DIMENSION(:,:), allocatable, intent(out) :: C
    end subroutine sub
end interface

REAL, DIMENSION(:,:), ALLOCATABLE :: A
REAL, DIMENSION(:,:), ALLOCATABLE :: B
REAL, DIMENSION(:,:), ALLOCATABLE :: C

CALL SUB(A,B,C)

write(*,*) size(A,1), ',', size(A,2)
write(*,*) size(B,1), ',', size(B,2)
write(*,*) size(C,1), ',', size(C,2)

END program PROG

subroutine sub(A,B,C)

REAL, DIMENSION(:,:), allocatable, intent(out) :: A
REAL, DIMENSION(:,:), allocatable, intent(out) :: B
REAL, DIMENSION(:,:), allocatable, intent(out) :: C

allocate(A(10,10))
allocate(B(20,20))
allocate(C(30,30))

end subroutine sub
2 Likes