From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=1.0 required=3.0 tests=BAYES_20,FROM_ADDR_WS autolearn=no autolearn_force=no version=3.4.5-pre1 Date: 4 Feb 93 17:24:38 GMT From: elroy.jpl.nasa.gov!news.aero.org!jordan@ames.arc.nasa.gov (Larry M. Jord an) Subject: Re: Need help with array passing Message-ID: <1krjgmINN33o@news.aero.org> List-Id: 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