Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

passing allocatable arrays to subroutines

passing allocatable arrays to subroutines

passing allocatable arrays to subroutines

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?

RE: passing allocatable arrays to subroutines

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
    allocate(a_gene(0:gene_size)) ! now allocate starting at 0 instead of 1
    write(*,*)allocated(many_genes),allocated(a_gene) ! shows if allocated
end program

subroutine set_size
    use numz
    write(*,*)'enter gene size:'
    write(*,*)'enter number of genes:'
end subroutine set_size

RE: passing allocatable arrays to subroutines

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.


module BoxMod
   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
   end subroutine BoxCreate

   subroutine BoxDelete (blist)
      integer, allocatable, intent (inout) :: blist(:)
      deallocate (blist)
   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

   do i = 1, amaxi, 1
      write (*,*) alist(i)
   call BoxDelete (alist)
end program main

RE: passing allocatable arrays to subroutines

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   smile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close