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!
  • Students Click Here

*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


eBook - Manufacturing the Cars of Tomorrow
In this ebook, we'll explore how additive manufacturing is going to transform the way cars are made. This includes commentary from thought leaders such as Ford's CTO, Ken Washington, Customer case studies of ways 3D printing is being used today, and a variety of part examples where 3D printing is already impacting how automobiles are made. Download Now
White Paper - Smart Manufacturing for Semiconductor
New technologies and approaches present great opportunities for semiconductor manufacturers to achieve high levels of innovation, yield and improvement. This white paper explores some of these cutting-edge technologies and how they can be applied effectively in the semiconductor industry. Read about how Smart Manufacturing is transforming the semiconductor industry. Download Now
White Paper - Analysis and Simulation in Aircraft Structure Certification
Organizations using simulation and analysis tools effectively see the benefits in their ability to achieve certification faster and with drastically less total cost than those who do not maximize these tools. Read this White Paper to learn about how digital tools such as analysis and simulation help in aircraft structure certification. 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