comp.lang.ada
 help / color / mirror / Atom feed
From: elroy.jpl.nasa.gov!news.aero.org!jordan@ames.arc.nasa.gov  (Larry M. Jord
Subject: Re: Need help with array passing
Date: 4 Feb 93 17:24:38 GMT	[thread overview]
Message-ID: <1krjgmINN33o@news.aero.org> (raw)

In article <1993Feb4.042524.7308@cs.wm.edu> egoforth@cs.wm.edu (Edward R. Gofor
th) writes:
>Ada afficionados, please help me!!
>
>I am writing a package which will include several procedures
>which need to receive an array whose size is not known at
>compile time.  The array will be indexed on the integers,
>and will be an array of integers.  The number of elements
>in the array will also be passed as an "in" parameter.
>The array itself will be modified and returned, and hence
>needs to be an "in out" parameter.  These procedures will
>be in a package, so I need both the specification and
>implementation declarations.
>
>Thank you for your help.  If possible, please e-mail your
>reply, as it is difficult for me to catch all of the
>articles in the newsgroup.
>
>Ed...
>
>Edward R. Goforth - egoforth@cs.wm.edu
>Department of Computer Science
>The College of William and Mary
>Williamsburg, Virginia
>
>

Ed, 

This is rather simple in Ada.  You will not need to pass
the size of the array around.  Ada handles this for you

First, define an unconstrained type to be used for passing 
array objects around:

  type IntegerArray is array (positive range <>) of integer;

Indeces can range over the positive integers 1..integer'last.

Then, define your procedures as follows.  (The following
example sums all elements of an array.)

  procedure Sum( anArray: in out IntegerArray; total: out integer ) is
    t: integer := 0;
  begin
    for i in anArray'range loop
      t := t + anArray(i);
    end loop;
    total := t;
  end Sum;

Use attributes ('range) to access array size information!

Be warned, in Ada only procedures can have 'in out' parameters.
A function to sum the array would look like this:

  function Sum( anArray: in IntegerArray ) return integer is
    t: integer := 0;
  begin
    for i in anArray'range loop
      t := t + anArray(i);
    end loop;
    return t;
  end Sum;

But note that the array is now 'in'!  Be sure to see how your
compiler passes array objects (by reference or value).  By
value could mean alot of copying of arrays, and this could
be expensive if they're big.

I hope you enjoy your Ada experience.  I have!

--Larry Jordan

             reply	other threads:[~1993-02-04 17:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-02-04 17:24 elroy.jpl.nasa.gov!news.aero.org!jordan [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-02-04  4:25 Need help with array passing Edwa rd R. Goforth
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox