From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 9 Oct 92 18:12:21 GMT From: seas.gwu.edu!mfeldman@uunet.uu.net (Michael Feldman) Subject: Re: Does the LRM permit parameter passing by reference? Message-ID: <1992Oct9.181221.8491@seas.gwu.edu> List-Id: 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." ------------------------------------------------------------------------