comp.lang.ada
 help / color / mirror / Atom feed
From: seas.gwu.edu!mfeldman@uunet.uu.net  (Michael Feldman)
Subject: Re: Does the LRM permit parameter passing by reference?
Date: 9 Oct 92 18:12:21 GMT	[thread overview]
Message-ID: <1992Oct9.181221.8491@seas.gwu.edu> (raw)

In article <1992Oct9.023807.20641@beaver.cs.washington.edu> simonson@cs.washing
ton.edu (Kevin Simonson) writes:
>
>     I was under the impression that Ada made local copies of parameter
>values when a procedure was called.  However my Verdix compiler apparently
>does calls by reference when the call is for a string value, as demonstrat-
>ed by the script file below.  Notice how the parameter "COPY" (which Ada
>won't allow code to alter inside the procedure) changes its value from the
>first "put_line" to the second.
>
>     Is it legal for Ada to allow this parameter to change in the middle of
>the procedure, or have I found a bug?
>
You've found a feature. The LRM explicitly allows _structured_ parameters
to be passed either by copy (value/result) or by reference. Scalars must
be copied.

Most compilers indeed pass arrays and records by reference; generally this
is thought to be more efficient, though there is reason to think that
the extra indirection could be more expensive than the time to copy.
Some compilers pass _small_ structures by copy and _large_ ones by
reference. Copying large structures consumes both time and space.

I recall a post a few years ago stating that this flexibility was needed
in order to allow reference passing on single processor machines, but
copying on distributed system, through task entry parameters.

IMHO, Ada has found a nice engineering compromise on the matter of
parameter passing: the abstraction (IN/OUT/IN OUT) is de-coupled from
the implementation (copy vs. reference). This is a clean break from
Pascal, which requires that non-VAR parameters be copied, no matter
how large. This means we had to teach our first-years to pass big
arrays as VAR parametrers, giving the danger of accidental modification,
in order to avoid endless copying.

Rich Pattis did a study once of Pascal texts' code for binary search,
in which much fuss was made over the log N search time, ignoring the
fact that copying the array into the search function was O(N).

Ada doesn't have this problem - we can be pretty confident that realistic
compilers will pass arrays by reference, even if they are IN parameters. 

Nice.

Mike Feldman

------------------------------------------------------------------------
Michael B. Feldman
co-chair, SIGAda Education Committee

Professor, Dept. of Electrical Engineering and Computer Science
School of Engineering and Applied Science
The George Washington University
Washington, DC 20052 USA
(202) 994-5253 (voice)
(202) 994-5296 (fax)
mfeldman@seas.gwu.edu (Internet)

"Americans wants the fruits of patience -- and they want them now."
------------------------------------------------------------------------

             reply	other threads:[~1992-10-09 18:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1992-10-09 18:12 Michael Feldman [this message]
  -- strict thread matches above, loose matches on Subject: below --
1992-10-13  4:16 Does the LRM permit parameter passing by reference? Michael Feldman
1992-10-11 16:20 eru.mt.luth.se!lunic!sunic!nobeltech!dawe
1992-10-09 14:11 Dan Rittersdorf
1992-10-09  2:38 Kevin Simonson
replies disabled

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