Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing allocatable arrays to subroutines

Status
Not open for further replies.

nanocomp

Mechanical
Joined
Sep 19, 2006
Messages
3
Location
US
I am trying to pass allocatable arrays declared on the main program to a subroutine and use this data inside the subroutine and then send it back to the main program. How do I declare the arrays inside the subroutine?

 
here's a simple example I found on the Net, for Fortran 90
module numz
integer, parameter:: b8 = selected_real_kind(14)
integer gene_size,num_genes
integer,allocatable :: a_gene(:),many_genes(:,:)
end module

program darwin
use numz
implicit none
integer ierr
call set_size()
allocate(a_gene(gene_size),stat=ierr) !stat= allows for an error code return
if(ierr /= 0)write(*,*)"allocation error" ! /= is .ne.
allocate(many_genes(gene_size,num_genes),stat=ierr) !2d array
if(ierr /= 0)write(*,*)"allocation error"
write(*,*)lbound(a_gene),ubound(a_gene) ! get lower and upper bound
! for the array
write(*,*)size(many_genes),size(many_genes,1) !get total size and size
!along 1st dimension
deallocate(many_genes) ! free the space for the array and matrix
deallocate(a_gene)
allocate(a_gene(0:gene_size)) ! now allocate starting at 0 instead of 1
write(*,*)allocated(many_genes),allocated(a_gene) ! shows if allocated
write(*,*)lbound(a_gene),ubound(a_gene)
end program


subroutine set_size
use numz
write(*,*)'enter gene size:'
read(*,*)gene_size
write(*,*)'enter number of genes:'
read(*,*)num_genes
end subroutine set_size
 
Here is another example for F95. It won't work in F90. This allocates within the routine but you can equally do it outside. All you said you were interested in was the declaration.
Code:
module BoxMod
contains
   subroutine BoxCreate (blist, bsize)
      ! Declare an allocatable parameter
      integer, allocatable, intent (inout) :: blist(:)
      integer, intent (in) :: bsize
      integer :: b
      allocate (blist(bsize))
      do b = 1, bsize
         blist(b) = 0
      enddo
      return
   end subroutine BoxCreate

   subroutine BoxDelete (blist)
      integer, allocatable, intent (inout) :: blist(:)
      deallocate (blist)
      return
   end subroutine BoxDelete
end module BoxMod

program main
   use BoxMod
   integer, dimension(:), allocatable :: alist
   integer:: amaxi
   parameter (amaxi = 20)
   call BoxCreate (alist, amaxi)
   do i = 1, amaxi, 1
      alist(i) = i * i
   enddo

   do i = 1, amaxi, 1
      write (*,*) alist(i)
   enddo
   call BoxDelete (alist)
   stop
end program main
 
You do it the same as you do any arrays. From the perspective of the Subroutine, the arrays it receives might as well be ones that were declared in the main routine without the allocatable attribute. There's no need for you to use modules or anything else to handle this situation. Keep in mind, we are talking about simple arrays of standard data types here, ie. Integer, Real, Character, etc. This doesn't apply to arrays of user defined Types.

Dan :-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top