comp.lang.ada
 help / color / mirror / Atom feed
From: Nick Roberts <nick.roberts@acm.org>
Subject: Re: dynamic array as return value?
Date: Thu, 14 Oct 2004 18:24:40 +0100
Date: 2004-10-14T18:24:40+01:00	[thread overview]
Message-ID: <2t7r2nF1s8m59U1@uni-berlin.de> (raw)
In-Reply-To: <cklea4$9eb$1@netnews.hinet.net>

bubble wrote:

> In Ada the bounds of an array need not be fixed at compile-time, as they can
> be specified by an object whose
> value is not fixed until run-time. Such an array is known as a dynamic
> array. However, once elaborated, the
> bounds of the dynamic array cannot be changed.

I think this could be put better, to be honest. In Ada, the bounds of an
array /value/ can be dynamic, but the bounds of an /object/ cannot change
dynamically. An 'object' is a variable, including a component of an array
or record value.

Since a function returns an object, the bounds of an array object
returned by a function cannot be changed dynamically. However, the
expression which is evaluated to construct the object which is returned
can have dynamically calculated bounds.

> Unlike many other languages, Ada allows a dynamic array to be returned
> as the result of a function. For example, a function Reverse_String
> can be written which reverses the characters passed to it.
> 
> An implementation of the function Reverse_String is as follows:
> 
> function Reverse_String( Str:in String ) return String is
>     Res : String( Str'Range ); --Dynamic bounds
> begin
>     for I in Str'Range loop
>         Res( Str'First+Str'Last-I ) := Str( I );
>     end loop;
>     return Res;
> end Reverse_String;

When this function executes the 'return' statement, the return expression
(Res) is evaluated, to construct an object (of the type String), and this
object is returned. The bounds are dynamically calculated, but once
calculated they cannot be changed.

Every time Reverse_String is called, the bounds of Res are evaluated
(dynamically), and Res then has these bounds, for the rest of its
lifetime -- which is the duration of the execution of the function -- and
once they are set, they cannot be changed during this lifetime.

So the bounds of the object Res cannot be changed dynamically (they can
be initially /set/ dynamically, but never /changed/ dynamically). Since
the return value is simply Res, this means that the bounds of the return
value are calculated dynamically (but then cannot be changed).

This is different to the strings of many other languages (e.g. BASIC),
in which you can dynamically change the upper bound of a variable at any
time (causing characters to be appended or truncated).

However, Ada supplies the library types
Ada.Strings.Bounded.Generic_Bounded_Strings.Bounded_String and
Ada.Strings.Unbounded.Unbounded_String. These strings types do allow
the upper bound of variables to be changed dynamically. It's just a
pity they have such long names!

> If ada allow dynamic array can be a return value,it mean bounds array
> can be return value?

Yes, the bounds -- both upper and lower -- of Str are passed into the
function  every time it is called, and both bounds of the return result
are also passed out. (It so happens, in this function, that these bounds
are the same.)

The fact that the bounds of Str are passed in is how Str'First, Str'Last,
and Str'Range can be used in the function.

I hope I've made things clear. Please ask if you have any more questions!

-- 
Nick Roberts



       reply	other threads:[~2004-10-14 17:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cklea4$9eb$1@netnews.hinet.net>
2004-10-14 17:24 ` Nick Roberts [this message]
2004-10-14 17:28 ` dynamic array as return value? Georg Bauhaus
2004-10-15  3:04 ` Steve
2004-10-15  6:02 ` bubble
2004-10-15 16:36   ` Georg Bauhaus
2004-10-19  5:35 ` bubble
replies disabled

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