comp.lang.ada
 help / color / mirror / Atom feed
* Beginner's question (array parameters to functions)
@ 1998-11-27  0:00 greg
  1998-11-28  0:00 ` Matthew Heaney
  1998-11-28  0:00 ` Tom Moran
  0 siblings, 2 replies; 8+ messages in thread
From: greg @ 1998-11-27  0:00 UTC (permalink / raw)


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.  The other details of the assignment (the
particular sorting method to use, etc.) are not pertinent to my question
so I do not mention them here.
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.

(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.)

Is my understanding correct, or is there some way to avoid having to use
two arrays?

g.




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

* Re: Beginner's question (array parameters to functions)
  1998-11-27  0:00 Beginner's question (array parameters to functions) greg
  1998-11-28  0:00 ` Matthew Heaney
@ 1998-11-28  0:00 ` Tom Moran
  1 sibling, 0 replies; 8+ messages in thread
From: Tom Moran @ 1998-11-28  0:00 UTC (permalink / raw)


Your understanding is correct.  There are ways around the 'in'
restriction, but you'll get to them later in the course.




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

* Re: Beginner's question (array parameters to functions)
  1998-11-27  0:00 Beginner's question (array parameters to functions) greg
@ 1998-11-28  0:00 ` Matthew Heaney
  1998-11-28  0:00   ` Brian Rogoff
  1998-11-29  0:00   ` Fergus Henderson
  1998-11-28  0:00 ` Tom Moran
  1 sibling, 2 replies; 8+ messages in thread
From: Matthew Heaney @ 1998-11-28  0:00 UTC (permalink / raw)


greg <ref15@columbia.edu> writes:

> 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, and one is
sorted.  Having two arrays is a condition of the problem statement, and
has nothing to do with the language.






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

* Re: Beginner's question (array parameters to functions)
  1998-11-28  0:00 ` Matthew Heaney
@ 1998-11-28  0:00   ` Brian Rogoff
  1998-11-29  0:00     ` Matthew Heaney
  1998-11-29  0:00   ` Fergus Henderson
  1 sibling, 1 reply; 8+ messages in thread
From: Brian Rogoff @ 1998-11-28  0:00 UTC (permalink / raw)


On Sat, 28 Nov 1998, Matthew Heaney wrote:
> greg <ref15@columbia.edu> writes:
> ... array sorting question snipped ...
> 
> But there are two arrays no matter what: one is unsorted, and one is
> sorted.  Having two arrays is a condition of the problem statement, and
> has nothing to do with the language.

The problem statement said nothing about whether the sort modified the
original array or not. Ada doesn't specify whether arrays are passed by 
reference; if you want to do a sort in place and return the same array 
then you'll use access types (pointers if you speak C) and work with 
references to the array. I consider it an excellent feature of Ada that
there is no extra syntax for this, unlike C. 

-- Brian






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

* Re: Beginner's question (array parameters to functions)
  1998-11-28  0:00   ` Brian Rogoff
@ 1998-11-29  0:00     ` Matthew Heaney
  0 siblings, 0 replies; 8+ messages in thread
From: Matthew Heaney @ 1998-11-29  0:00 UTC (permalink / raw)


Brian Rogoff <bpr@shell5.ba.best.com> writes:

> On Sat, 28 Nov 1998, Matthew Heaney wrote:
> > greg <ref15@columbia.edu> writes:
> > ... array sorting question snipped ...
> > 
> > But there are two arrays no matter what: one is unsorted, and one is
> > sorted.  Having two arrays is a condition of the problem statement, and
> > has nothing to do with the language.
> 
> The problem statement said nothing about whether the sort modified the
> original array or not. Ada doesn't specify whether arrays are passed by 
> reference; if you want to do a sort in place and return the same array 
> then you'll use access types (pointers if you speak C) and work with 
> references to the array. I consider it an excellent feature of Ada that
> there is no extra syntax for this, unlike C. 

The problem was: "implement a sort routine as a function, that takes an
unsorted array as the parameter, and returns a sorted array as a
result."

Something like:

   function Sort (Items : Item_Array) return Item_Array;

This problem requires two arrays.

It may be that "the problem statement said nothing about whether the
sort modified the original array or not," but the implicit requirement
for a pair of arrays is derived from the explicit requirement that the
sort be implemented as a function.












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

* Re: Beginner's question (array parameters to functions)
  1998-11-28  0:00 ` Matthew Heaney
  1998-11-28  0:00   ` Brian Rogoff
@ 1998-11-29  0:00   ` Fergus Henderson
  1998-11-29  0:00     ` Matthew Heaney
  1 sibling, 1 reply; 8+ messages in thread
From: Fergus Henderson @ 1998-11-29  0:00 UTC (permalink / raw)


Matthew Heaney <matthew_heaney@acm.org> writes:

>greg <ref15@columbia.edu> 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.
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.

In other languages, e.g. Clean and Mercury, arrays can't be modified,
but it is possible to create new arrays by modifying existing arrays,
so long as the old version will no longer be referenced.  In these
languages, the two arrays would be conceptually distinct, but could
occupy the same storage area.

--
Fergus Henderson <fjh@cs.mu.oz.au>  |  "Binaries may die
WWW: <http://www.cs.mu.oz.au/~fjh>  |   but source code lives forever"
PGP: finger fjh@128.250.37.3        |     -- leaked Microsoft memo.




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

* Re: Beginner's question (array parameters to functions)
  1998-11-29  0:00   ` Fergus Henderson
@ 1998-11-29  0:00     ` Matthew Heaney
  1998-12-02  0:00       ` Fergus Henderson
  0 siblings, 1 reply; 8+ messages in thread
From: Matthew Heaney @ 1998-11-29  0:00 UTC (permalink / raw)


fjh@cs.mu.oz.au (Fergus Henderson) writes:

> Matthew Heaney <matthew_heaney@acm.org> writes:
> 
> >greg <ref15@columbia.edu> 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.





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

* Re: Beginner's question (array parameters to functions)
  1998-11-29  0:00     ` Matthew Heaney
@ 1998-12-02  0:00       ` Fergus Henderson
  0 siblings, 0 replies; 8+ messages in thread
From: Fergus Henderson @ 1998-12-02  0:00 UTC (permalink / raw)


Matthew Heaney <matthew_heaney@acm.org> writes:

 >fjh@cs.mu.oz.au (Fergus Henderson) writes:
 >
 >> Matthew Heaney <matthew_heaney@acm.org> writes:
 >> 
 >> >greg <ref15@columbia.edu> 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.
 >> ...
 >> >> is there some way to avoid having to use two arrays?
...
 >> >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.

That conclusion ("Therefore, ...") may be true if you restrict yourself to
Ada.  But there are other languages for which it is not true, as I
demonstrated by the examples in my reply.  Thus your statement that
"it has nothing to do with the language" is not correct.

 >> 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.

Sorry -- I meant to write "contradiction" instead of "contraction".

--
Fergus Henderson <fjh@cs.mu.oz.au>  |  "Binaries may die
WWW: <http://www.cs.mu.oz.au/~fjh>  |   but source code lives forever"
PGP: finger fjh@128.250.37.3        |     -- leaked Microsoft memo.




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

end of thread, other threads:[~1998-12-02  0:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-11-27  0:00 Beginner's question (array parameters to functions) greg
1998-11-28  0:00 ` Matthew Heaney
1998-11-28  0:00   ` Brian Rogoff
1998-11-29  0:00     ` Matthew Heaney
1998-11-29  0:00   ` Fergus Henderson
1998-11-29  0:00     ` Matthew Heaney
1998-12-02  0:00       ` Fergus Henderson
1998-11-28  0:00 ` Tom Moran

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