comp.lang.ada
 help / color / mirror / Atom feed
* "out" parameters
@ 2004-05-20 23:37 Jo
  2004-05-20 23:53 ` Ludovic Brenta
                   ` (3 more replies)
  0 siblings, 4 replies; 15+ messages in thread
From: Jo @ 2004-05-20 23:37 UTC (permalink / raw)


Does anyone happen to know whether an "out" parameter in Ada is being
passed by-copy or by-reference?

If the parameter is a record with neither elementary nor access type
component in it, then is it true that whether the "out" parameter is
passed by-copy or by-reference is totally compiler dependent?



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

* Re: "out" parameters
  2004-05-20 23:37 "out" parameters Jo
@ 2004-05-20 23:53 ` Ludovic Brenta
  2004-05-21  0:33   ` Georg Bauhaus
  2004-05-21 19:17   ` Keith Thompson
  2004-05-21  1:09 ` Jeffrey Carter
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 15+ messages in thread
From: Ludovic Brenta @ 2004-05-20 23:53 UTC (permalink / raw)


Jo writes:
> Does anyone happen to know whether an "out" parameter in Ada is
> being passed by-copy or by-reference?
>
> If the parameter is a record with neither elementary nor access type
> component in it, then is it true that whether the "out" parameter is
> passed by-copy or by-reference is totally compiler dependent?

Yes, the compiler decides.  Only for tagged types, parameters are
passed by reference no matter what the mode.

Besides by-copy and by-reference, there is also the possibility of
passing parameters in processor registers.

The mechanism chosen also depends on whether or not the called
subprogram is inlined.

-- 
Ludovic Brenta.



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

* Re: "out" parameters
  2004-05-20 23:53 ` Ludovic Brenta
@ 2004-05-21  0:33   ` Georg Bauhaus
  2004-05-21 19:17   ` Keith Thompson
  1 sibling, 0 replies; 15+ messages in thread
From: Georg Bauhaus @ 2004-05-21  0:33 UTC (permalink / raw)


Ludovic Brenta <ludovic.brenta@insalien.org> wrote:
: Jo writes:

:> If the parameter is a record with neither elementary nor access type
:> component in it, then is it true that whether the "out" parameter is
:> passed by-copy or by-reference is totally compiler dependent?
: 
: Yes, the compiler decides.  Only for tagged types, parameters are
: passed by reference no matter what the mode.

You might also want to skim the reference manual which tells
you about by-reference types, and by-reference return types.
It is all pretty well defined, i.e. the compiler knows when
it can decide and when not.

Georg



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

* Re: "out" parameters
  2004-05-20 23:37 "out" parameters Jo
  2004-05-20 23:53 ` Ludovic Brenta
@ 2004-05-21  1:09 ` Jeffrey Carter
  2004-05-21  9:28 ` Martin Krischik
  2004-06-04 21:59 ` Nick Roberts
  3 siblings, 0 replies; 15+ messages in thread
From: Jeffrey Carter @ 2004-05-21  1:09 UTC (permalink / raw)


Jo wrote:

> Does anyone happen to know whether an "out" parameter in Ada is being
> passed by-copy or by-reference?
> 
> If the parameter is a record with neither elementary nor access type
> component in it, then is it true that whether the "out" parameter is
> passed by-copy or by-reference is totally compiler dependent?

This was discussed to death recently, and several times before that. The 
parameter mode ("in", "in out", "out") has nothing to do with the 
parameter passing mechanism.

The simple version of the rules is: Elementary types are always passed 
by copy. Tagged and limited types are always passed by reference. The 
compiler chooses the parameter passing mechanism for all other types. 
The software engineer need rarely be concerned what mechanism the 
compiler chooses.

-- 
Jeff Carter
"C++ is like jamming a helicopter inside a Miata
and expecting some sort of improvement."
Drew Olbrich
51




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

* Re: "out" parameters
  2004-05-20 23:37 "out" parameters Jo
  2004-05-20 23:53 ` Ludovic Brenta
  2004-05-21  1:09 ` Jeffrey Carter
@ 2004-05-21  9:28 ` Martin Krischik
  2004-05-28 23:14   ` Jo
  2004-06-04 21:59 ` Nick Roberts
  3 siblings, 1 reply; 15+ messages in thread
From: Martin Krischik @ 2004-05-21  9:28 UTC (permalink / raw)


Jo wrote:

> Does anyone happen to know whether an "out" parameter in Ada is being
> passed by-copy or by-reference?
> 
> If the parameter is a record with neither elementary nor access type
> component in it, then is it true that whether the "out" parameter is
> passed by-copy or by-reference is totally compiler dependent?

It is not compiler dependent. There are rules. Within the rules the compiler
will do what is best.

If you need to force him to use a specific calling convetion you can use
"pragma Convention" or "pragma Export" described in the chapter for
interfacing with other languages. But don't do it unless you have too or
for training.

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




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

* Re: "out" parameters
  2004-05-20 23:53 ` Ludovic Brenta
  2004-05-21  0:33   ` Georg Bauhaus
@ 2004-05-21 19:17   ` Keith Thompson
  2004-05-21 22:25     ` Vinzent 'Gadget' Hoefler
                       ` (2 more replies)
  1 sibling, 3 replies; 15+ messages in thread
From: Keith Thompson @ 2004-05-21 19:17 UTC (permalink / raw)


Ludovic Brenta <ludovic.brenta@insalien.org> writes:
[...]
> Besides by-copy and by-reference, there is also the possibility of
> passing parameters in processor registers.

Isn't that just a special case of by-copy?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.



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

* Re: "out" parameters
  2004-05-21 19:17   ` Keith Thompson
@ 2004-05-21 22:25     ` Vinzent 'Gadget' Hoefler
  2004-05-22  8:20     ` Martin Krischik
  2004-05-25 20:34     ` Robert I. Eachus
  2 siblings, 0 replies; 15+ messages in thread
From: Vinzent 'Gadget' Hoefler @ 2004-05-21 22:25 UTC (permalink / raw)


Keith Thompson wrote:

>Ludovic Brenta <ludovic.brenta@insalien.org> writes:
>[...]
>> Besides by-copy and by-reference, there is also the possibility of
>> passing parameters in processor registers.
>
>Isn't that just a special case of by-copy?

Well, it could as well be a special case of by-reference. ;)


Vinzent.



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

* Re: "out" parameters
  2004-05-21 19:17   ` Keith Thompson
  2004-05-21 22:25     ` Vinzent 'Gadget' Hoefler
@ 2004-05-22  8:20     ` Martin Krischik
  2004-05-25 20:34     ` Robert I. Eachus
  2 siblings, 0 replies; 15+ messages in thread
From: Martin Krischik @ 2004-05-22  8:20 UTC (permalink / raw)


Keith Thompson wrote:

> Ludovic Brenta <ludovic.brenta@insalien.org> writes:
> [...]
>> Besides by-copy and by-reference, there is also the possibility of
>> passing parameters in processor registers.
> 
> Isn't that just a special case of by-copy?

Depends if a reference or a copy is passes within the register ;-).
 
With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




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

* Re: "out" parameters
  2004-05-21 19:17   ` Keith Thompson
  2004-05-21 22:25     ` Vinzent 'Gadget' Hoefler
  2004-05-22  8:20     ` Martin Krischik
@ 2004-05-25 20:34     ` Robert I. Eachus
  2 siblings, 0 replies; 15+ messages in thread
From: Robert I. Eachus @ 2004-05-25 20:34 UTC (permalink / raw)




Keith Thompson wrote:
> Ludovic Brenta <ludovic.brenta@insalien.org> writes:
> [...]
> 
>>Besides by-copy and by-reference, there is also the possibility of
>>passing parameters in processor registers.
> 
> 
> Isn't that just a special case of by-copy?

Technically,  no.  An argument can be passed by reference in a register, 
even though the reference is implicit in the code.  For example you 
could have a bit array in a register, and pass the index of the bit that 
was a parameter.

The only reason this matters is that there are hardware registers which 
may be implicit or explicit arguments to OS routines.  The semantics are 
by reference, even though the registers are defined by the hardware.

-- 

                                           Robert I. Eachus

"The terrorists rejoice in the killing of the innocent, and have 
promised similar violence against Americans, against all free peoples, 
and against any Muslims who reject their ideology of murder. Their 
barbarism cannot be appeased, and their hatred cannot be satisfied. 
There's only one way to deal with terror: We must confront the enemy and 
stay on the offensive until these killers are defeated." -- George W. Bush




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

* Re: "out" parameters
  2004-05-21  9:28 ` Martin Krischik
@ 2004-05-28 23:14   ` Jo
  2004-05-28 23:45     ` Marius Amado Alves
                       ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Jo @ 2004-05-28 23:14 UTC (permalink / raw)


Martin Krischik <krischik@users.sourceforge.net> wrote in message news:<14098638.pGWFRt1ihv@linux1.krischik.com>...
> Jo wrote:
> 
> > Does anyone happen to know whether an "out" parameter in Ada is being
> > passed by-copy or by-reference?
> > 
> > If the parameter is a record with neither elementary nor access type
> > component in it, then is it true that whether the "out" parameter is
> > passed by-copy or by-reference is totally compiler dependent?
> 
> It is not compiler dependent. There are rules. Within the rules the compiler
> will do what is best.
> 
> If you need to force him to use a specific calling convetion you can use
> "pragma Convention" or "pragma Export" described in the chapter for
> interfacing with other languages. But don't do it unless you have too or
> for training.
> 
> With Regards
> 
> Martin


If the compiler, within the rules, does what it thinks it's the best,
it implies that it is, at least to a certain extent, compiler
dependent.

If I have a record that contains 2 fields, I initialize the 1st field,
I then pass the record as an 'out' parameter to a subprogram.  The
subprogram assigns a value to the 2nd field and returns.

If the record has neither elementary nor access type in it, then the
compiler may have the flexibility to decide how it wants to pass the
record.

The question is, what value should I expect for the 1st field in the
record when the subprogram returns?

Regards,
Jo



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

* Re: "out" parameters
  2004-05-28 23:14   ` Jo
@ 2004-05-28 23:45     ` Marius Amado Alves
  2004-05-29  1:45     ` Jeffrey Carter
  2004-05-29  7:15     ` Martin Krischik
  2 siblings, 0 replies; 15+ messages in thread
From: Marius Amado Alves @ 2004-05-28 23:45 UTC (permalink / raw)
  To: comp.lang.ada

> If I have a record that contains 2 fields, I initialize the 1st field,
> I then pass the record as an 'out' parameter to a subprogram.  The
> subprogram assigns a value to the 2nd field and returns.
>
> If the record has neither elementary nor access type in it, then the
> compiler may have the flexibility to decide how it wants to pass the
> record.
>
> The question is, what value should I expect for the 1st field in the
> record when the subprogram returns?

This is defined by the language, it's not compiler-dependent.

If you mean "in out" (and not just "out"), you get out whatever you passed
in in any component and the subprogram did not touch.

If you really mean (just) "out", well, you cannot expect to get out anything
valid that the subprogram did not touch, can you? After all, you're not
passing anything "in".

(Well, actually you may be kind of passing something, e.g. bounds. But never
field values.)




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

* Re: "out" parameters
  2004-05-28 23:14   ` Jo
  2004-05-28 23:45     ` Marius Amado Alves
@ 2004-05-29  1:45     ` Jeffrey Carter
  2004-05-29  7:15     ` Martin Krischik
  2 siblings, 0 replies; 15+ messages in thread
From: Jeffrey Carter @ 2004-05-29  1:45 UTC (permalink / raw)


Jo wrote:

> If the record has neither elementary nor access type in it, then the
> compiler may have the flexibility to decide how it wants to pass the
> record.

If the record is neither tagged nor limited, the passing mechanism is 
chosen by the compiler, regardless of the types of components it contains.

> The question is, what value should I expect for the 1st field in the
> record when the subprogram returns?

You should not expect anything.

-- 
Jeff Carter
"My mind is aglow with whirling, transient nodes of
thought, careening through a cosmic vapor of invention."
Blazing Saddles
85




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

* Re: "out" parameters
  2004-05-28 23:14   ` Jo
  2004-05-28 23:45     ` Marius Amado Alves
  2004-05-29  1:45     ` Jeffrey Carter
@ 2004-05-29  7:15     ` Martin Krischik
  2 siblings, 0 replies; 15+ messages in thread
From: Martin Krischik @ 2004-05-29  7:15 UTC (permalink / raw)


Jo wrote:

> Martin Krischik <krischik@users.sourceforge.net> wrote in message
> news:<14098638.pGWFRt1ihv@linux1.krischik.com>...
>> Jo wrote:
>> 
>> > Does anyone happen to know whether an "out" parameter in Ada is being
>> > passed by-copy or by-reference?
>> > 
>> > If the parameter is a record with neither elementary nor access type
>> > component in it, then is it true that whether the "out" parameter is
>> > passed by-copy or by-reference is totally compiler dependent?
>> 
>> It is not compiler dependent. There are rules. Within the rules the
>> compiler will do what is best.
>> 
>> If you need to force him to use a specific calling convetion you can use
>> "pragma Convention" or "pragma Export" described in the chapter for
>> interfacing with other languages. But don't do it unless you have too or
>> for training.
>> 
>> With Regards
>> 
>> Martin
> 
> 
> If the compiler, within the rules, does what it thinks it's the best,
> it implies that it is, at least to a certain extent, compiler
> dependent.
> 
> If I have a record that contains 2 fields, I initialize the 1st field,
> I then pass the record as an 'out' parameter to a subprogram.  The
> subprogram assigns a value to the 2nd field and returns.

> If the record has neither elementary nor access type in it, then the
> compiler may have the flexibility to decide how it wants to pass the
> record.

> The question is, what value should I expect for the 1st field in the
> record when the subprogram returns?

The first field is initialised to is default value - which might be
undefined - the second to whatever you said.

With "out" a brand new record is created and copied back into the one
passed.

The compiler might optimise on the amount of copy needed but this is what
you should expect.

If you have:

procedure X (Ret : out Some_Type);
function X return Some_Type;

and both X will perform the same operations then

X (Some_Var);
Some_Var := X

will be the same. Only that with Ada < AdaY0 you the procedure can be used
for limited types and the function can not. 

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




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

* Re: "out" parameters
  2004-05-20 23:37 "out" parameters Jo
                   ` (2 preceding siblings ...)
  2004-05-21  9:28 ` Martin Krischik
@ 2004-06-04 21:59 ` Nick Roberts
  2004-06-18  0:58   ` Jo
  3 siblings, 1 reply; 15+ messages in thread
From: Nick Roberts @ 2004-06-04 21:59 UTC (permalink / raw)


"Jo" <cyanobact@hotmail.com> wrote in message
news:e26e273.0405201537.35ced9db@posting.google.com...

> Does anyone happen to know whether an "out" parameter in Ada
> is being passed by-copy or by-reference?
>
> If the parameter is a record with neither elementary nor access type
> component in it, then is it true that whether the "out" parameter is
> passed by-copy or by-reference is totally compiler dependent?

I think that is true, yes.

However, I think possibly you (Jo) should bear in mind that the parameter
passing mechanism does not normally have any semantic significance in Ada,
unlike many other languages. For example, in C:

   void foo(int bar, int &hum) {
      bar++;
      hum++;
   };

   int a = 0;
   int b = 0;

   ...
      foo(a,b);

will cause b to be incremented (to 1) but will not change a.

In Ada:

   procedure Foo (Bar: in Integer; Hum: in out Integer) is
   begin
      Bar := Bar + 1;
      Hum := Hum + 1;
   end;

will not compile. The compiler will complain that Bar is a constant, so it
cannot be assigned to.

If you were to write:

   type My_Rec_1 is record ... end record;
   X1: constant My_Rec_1 := ...;
   type My_Rec_2 is record A, B: My_Rec_1; end record;
   X2: My_Rec_2;

   procedure Init (Rec: out My_Rec_2) is
   begin
      Rec.A := X1;
   end;

   ...
      Init(X2);

then at this point you can be sure that X2.A will have the value of X1 in
it, regardless of the parameter passing mechanism. You cannot be sure what
value X2.B has.

Most of the time, Ada tries to minimise nasty surprises!

In case you are curious, one situation in which the parameter passing method
matters in Ada is when two tasks call a procedure, and their executions of
the procedure overlap. If both tasks pass the same variable as a parameter,
and the procedure could update the parameter, the effect on the variable
could depend on whether it is passed by copy or by reference. The choice of
mechanism might be a compiler choice. A dependency on a compiler choice in
an Ada program is erroneous (if it is intended to be portable or
long-lived), but such errors cannot generally be detected by the compiler.

HTH

-- 
Nick Roberts





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

* Re: "out" parameters
  2004-06-04 21:59 ` Nick Roberts
@ 2004-06-18  0:58   ` Jo
  0 siblings, 0 replies; 15+ messages in thread
From: Jo @ 2004-06-18  0:58 UTC (permalink / raw)


"Nick Roberts" <nick.roberts@acm.org> wrote in message news:<2ic9mcFl05rsU1@uni-berlin.de>...

> ....
>The choice of
> mechanism might be a compiler choice. A dependency on a compiler choice in
> an Ada program is erroneous (if it is intended to be portable or
> long-lived), but such errors cannot generally be detected by the compiler.
> 
> HTH

Thanks for the very clear explanation Nick.

I totally agree that one should use the language the way that it is
designed for, rather than having it to depend on how a particular
compiler works.  This will definitely improve the longevity of the
program, and probably also the longevity of the developer.

Jo



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

end of thread, other threads:[~2004-06-18  0:58 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-20 23:37 "out" parameters Jo
2004-05-20 23:53 ` Ludovic Brenta
2004-05-21  0:33   ` Georg Bauhaus
2004-05-21 19:17   ` Keith Thompson
2004-05-21 22:25     ` Vinzent 'Gadget' Hoefler
2004-05-22  8:20     ` Martin Krischik
2004-05-25 20:34     ` Robert I. Eachus
2004-05-21  1:09 ` Jeffrey Carter
2004-05-21  9:28 ` Martin Krischik
2004-05-28 23:14   ` Jo
2004-05-28 23:45     ` Marius Amado Alves
2004-05-29  1:45     ` Jeffrey Carter
2004-05-29  7:15     ` Martin Krischik
2004-06-04 21:59 ` Nick Roberts
2004-06-18  0:58   ` Jo

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