comp.lang.ada
 help / color / mirror / Atom feed
* Need help with array passing
@ 1993-02-04  4:25 Edwa rd R. Goforth
  0 siblings, 0 replies; 2+ messages in thread
From: Edwa rd R. Goforth @ 1993-02-04  4:25 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Need help with array passing
@ 1993-02-04 17:24 elroy.jpl.nasa.gov!news.aero.org!jordan
  0 siblings, 0 replies; 2+ messages in thread
From: elroy.jpl.nasa.gov!news.aero.org!jordan @ 1993-02-04 17:24 UTC (permalink / 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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1993-02-04 17:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-02-04 17:24 Need help with array passing elroy.jpl.nasa.gov!news.aero.org!jordan
  -- strict thread matches above, loose matches on Subject: below --
1993-02-04  4:25 Edwa rd R. Goforth

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