From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,99222a5bd46ef3c9 X-Google-Attributes: gid103376,public From: vsnyder@gyre.jpl.nasa.gov (Van Snyder) Subject: Function result Date: 1997/06/25 Message-ID: <5os5r8$4ud@netline.jpl.nasa.gov>#1/1 X-Deja-AN: 252889210 References: <19970619161801.MAA18772@ladder02.news.aol.com> Organization: Jet Propulsion Laboratory Newsgroups: comp.lang.ada Date: 1997-06-25T00:00:00+00:00 List-Id: In article <19970619161801.MAA18772@ladder02.news.aol.com>, johnherro@aol.com (John Herro) writes: |> bobduff@world.std.com (Robert A Duff) writes: |> > Of course, for functions, Ada doesn't allow you to set the result |> > without doing a return. Which I've sometimes found annoying. |> |> But you can set a local variable Result in as many places as you want |> and later return Result. Isn't that as good? I do it all the time. This _works_ but it would be nice if you didn't need to trust the compiler to optimize away the assignments to Result. It doesn't seem so bad in Ada, with the explicit copy-out model of returning function results, because you're only doubling the cost of returning the function result. The copy-out model, however, causes other headaches. In particular, you can't return a limited type, except by the "return by reference" kludge. In Pascal and Fortran (at least) one returns a function result by assigning to the result variable (default name = function name in Fortran 95). The Fortran standard is silent on _how_ function results are returned. For composite results (e.g. structures, arrays, character strings), most compilers pass the address where the function result is to land as a "hidden argument." In this way, the syntactic device of assigning to the function result _really_is_ assigning directly to the function result -- no extra copying involved. By the way, this would eliminate the problem about returning a limited type in Ada. In the next standardization of Ada, I suggest allowing an optional "name:" after "return" in a function header. This would imply four things: 1. The client is expected to supply the address where the function result is desired as a "hidden argument". 2. The function result is materialized directly in the place the client wants it, without any copying. 3. Since there's no copying going on outside the function, it would be OK to allow returning a limited type. 4. In the function, the result would be materialized by storing into the result variable, not by mentioning the value in a "return" statement. The program could do this piece-by-piece in the case of a composite result. -- What fraction of Americans believe | Van Snyder Wrestling is real and NASA is fake? | vsnyder@math.jpl.nasa.gov