comp.lang.ada
 help / color / mirror / Atom feed
* "out" and  "in out"
@ 2004-07-26  9:58 zork
  2004-07-26 11:00 ` David C. Hoos
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: zork @ 2004-07-26  9:58 UTC (permalink / raw)


Hi i found the following explaination:

In Ada, "in" parameters are similar to C++ const parameters. They are
effectively read-only within the scope of the called subprogram.
Ada "in out" parameters have a reliable initial value (that passed
in from the calling subprogram) and may be modified within the scope
of the called procedure. Ada "out" parameters have no reliable
initial value, but are expected to be assigned a value within the
called procedure.

What does "have no reliable initial value" mean when considering the "out"
parameter?

By chance I created a small program as follows:

===========
s : string := "CAT";

procedure modify ( s1 : out string ) is
begin
   s1(2) := 'U';
end modify;

..

put ( modify(s) );
===========

now I get as a result "CUT", and i dont understand why i get this result.
Doesnt the "out" specify that its initial value isnt passed in via the
parameter? But it seems to be passed in the above. In fact the "out" is
acting like an "in out". I am a little confused. Could someone shed some
light on this?

Many thanks!

zork






^ permalink raw reply	[flat|nested] 7+ messages in thread
* Re: "out" and  "in out"
@ 2004-07-26 10:54 Christoph Karl Walter Grein
  2004-07-26 14:16 ` Marc A. Criley
  0 siblings, 1 reply; 7+ messages in thread
From: Christoph Karl Walter Grein @ 2004-07-26 10:54 UTC (permalink / raw)
  To: comp.lang.ada

"no reliable initial value" means, you cannot rely on the value, but it may have a value. This depends on the parameter passing mechanism, which is _not_ related to the parameter mode (contrary to what many people think).

So the parameter mode is there (nearly) solely for the information of the reader. The parameter passing mechanism for all kinds of parameters is defined in the RM. There are parameters passed by copy (in and out), by reference; for some it is explicitly left undefined.

In your case, the passing mechanism is by reference, so you get what you get. But don't rely on this, rely only on the mode, i.e. the parameter st is undefined upon entering the procedure modify, so when you only write component 2, upon return, only component 2 has been written. Under slight variations, the result of your code might be <garbage character>U<garbage character>.

Another thing: Don't rely upon s1'First = 1. Component 2 might not exist. You could call modify like so:

X: String (25..30);
modify (X);

Now s1(2) inevitably will raise Constraint_Error.
____________________________________________________
Aufnehmen, abschicken, nah sein - So einfach ist 
WEB.DE Video-Mail: http://freemail.web.de/?mc=021200




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

end of thread, other threads:[~2004-07-28 14:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-26  9:58 "out" and "in out" zork
2004-07-26 11:00 ` David C. Hoos
2004-07-26 11:30 ` Martin Krischik
2004-07-26 14:46 ` Nick Roberts
2004-07-28 14:07   ` zork
  -- strict thread matches above, loose matches on Subject: below --
2004-07-26 10:54 Christoph Karl Walter Grein
2004-07-26 14:16 ` Marc A. Criley

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