From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,998965b11075593f X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: Beginner's question (array parameters to functions) Date: 1998/11/29 Message-ID: #1/1 X-Deja-AN: 416712416 Sender: matt@mheaney.ni.net References: <365F4360.F7E85C1E@columbia.edu> <73qc0p$24e$1@mulga.cs.mu.OZ.AU> NNTP-Posting-Date: Sun, 29 Nov 1998 00:26:18 PDT Newsgroups: comp.lang.ada Date: 1998-11-29T00:00:00+00:00 List-Id: fjh@cs.mu.oz.au (Fergus Henderson) writes: > Matthew Heaney writes: > > >greg writes: > >> One requirement of the assignment is that the > >> sorting be done by a function which takes the array as a parameter and > >> returns a sorted array. > ... > >> My question is the following: As I understand it, in an Ada function, a > >> parameter must be passed in "in" mode. Therefore, I assume that in this > >> assignment, the sorting function has to make a copy of the array > >> parameter, sort the items in this second array, and finally return this > >> second array to the caller subprogram, because, given that the array is > >> passed as a parameter, there is no way to change the values of the array > >> that is copied in for use in the function. > >> > >> Is my understanding correct, or is there some way to avoid having to use > >> two arrays? > > > >But there are two arrays no matter what: one is unsorted > > When the sort function is called. > > >and one is sorted. > > When the sort function returns. > > >Having two arrays is a condition of the problem statement, > >and has nothing to do with the language. > > This is not correct. > > In some languages, e.g. C++, arrays can be passed and returned by > reference, and can be modified, so (unless there was some additional > constraint in the assignment, e.g. that the function's input argument > not be modified) the two arrays could be the same array. That is precisely that case. The requirement is for a function, not a procedure. Therefore, there is a requirement that the original array not be modified. The problem is perhaps more clearly stated as like this. "Given this specification of this Ada function: function Sort (Items : Item_Array) return Item_Array; Implement the body of this Ada function." Implementing the body Sort requires implies the existence of another array; specifically, the return value of the function. When you call the sort function: First_Array_Which_Is_Unsorted : Item_Array; ... Second_Array_Which_Is_Sorted : constant Item_Array := Sort (First_Array_Which_Is_Unsorted); There are two arrays. Here are excepts from the original post: (start of quote) I have a homework assignment which involves writing an Ada program that sorts an array of items. One requirement of the assignment is that the sorting be done by a function which takes the array as a parameter and returns a sorted array. My question is the following: As I understand it, in an Ada function, a parameter must be passed in "in" mode. Therefore, I assume that in this assignment, the sorting function has to make a copy of the array parameter, sort the items in this second array, and finally return this second array to the caller subprogram, because, given that the array is passed as a parameter, there is no way to change the values of the array that is copied in for use in the function. [Matt - yes, everything in the paragraph is true.] (I realize that it would be possible to use a procedure instead of a function, or to use a global variable rather than pass any parameter to the sorting function, but let's assume that that would not be acceptable in this assignment.) [Matt - here is the explicit statement on the requirement for a function, not a procedure.] Is my understanding correct, or is there some way to avoid having to use two arrays? [Matt - yes, your understanding is correct.] (end of quote) > The fact that these two arrays have different contents is not a contraction, > because the different contents occur at different times, and it's quite > possible for a single array to have different contents at different times. I don't understand this paragraph.