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


White Paper - Strategies to Secure Connected Cars with Firewalls
White-hat hackers have demonstrated gaining remote access to dashboard functions and transmissions of connected vehicles. That makes a firewall a vital component of a multilayered approach to vehicle security as well as overall vehicle safety and reliability. Learn strategies to secure with firewalls. Download Now
White Paper - Model Based Engineering for Wire Harness Manufacturing
As complexity rises, current harness manufacturing methods are struggling to keep pace due to manual data exchanges and the inability to capture tribal knowledge. A model-based wire harness manufacturing engineering flow automates data exchange and captures tribal knowledge through design rules to help harness manufacturers improve harness quality and boost efficiency. Download Now
White Paper - What is Generative Design and Why Do You Need It?
Engineers are being asked to produce more sophisticated designs under a perfect storm of complexity, cost, and change management pressures. Generative design empowers automotive design teams to navigate this storm by employing automation, data re-use and synchronization, and framing design in the context of a full vehicle platform. Download Now
eBook - Simulation-Driven Design with SOLIDWORKS
Simulation-driven design can reduce the time and cost of product development. In this engineering.com eBook, we’ll explore how SOLIDWORKS users can access simulation-driven design through the SOLIDWORKS Simulation suite of analysis tools. 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