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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8e64f4db20d57eb5,start X-Google-Attributes: gid103376,public From: Christopher Felaco Subject: Call by reference vs. call by value Date: 1996/07/20 Message-ID: <31F10E50.726@egr.uri.edu>#1/1 X-Deja-AN: 169042998 content-type: text/plain; charset=us-ascii organization: University of Rhode Island mime-version: 1.0 reply-to: felaco@egr.uri.edu newsgroups: comp.lang.ada x-mailer: Mozilla 3.0b5a (Win95; I) Date: 1996-07-20T00:00:00+00:00 List-Id: I came across a problem with a procedure that resembled the following: type Simple_Vector is array (1 .. 2) of Float; procedure Vector_Manipulation (X : in Simple_Vector; Y : out Simple_Vector) is begin Y (1) := X (1) + X (2); Y (2) := X (1) * X (2); end Vector_Manipulation; I was told that calling this procedure with the same in and out parameters as follows would give unexpected results: Vector_Manipulation (X => Test_Vector, Y => Text_Vector); The reason is that both parameters may be passed by reference, hence modifying Y in the first line, modifies X as well, and the value of X used in the second line is not what was desired. I was surprised by this, it sounds like a typical problem in C or C++, not something likely to happen in Ada. So I did some research and concluded from section 6.2 of the Ada 95 RM that it is entirely up to the compiler to determine how arrays of simple elementary values are called. I recall also reading that ACT got itself in a bit of trouble by changing the behavior of gnat to use call by reference for all records. My reasons for posting this are twofold. The first reason is to verify that this is correct, and see if anyone else has been bitten by this particular undefined behavior. The second reason is to express some disappointment. I think in this area, C++ has a big advantage in that it forces the programmer to be aware of whether a function uses call-by-refernce of call-by-value semantics. This is somewhat of a pain at times, but at least it is obvious and clearly defined. I had no idea that Ada's behavior was undefined and wrongly assumed that in parameters were always passed by value. I realize that the solution to this problem in this case is to simply make one assignment using an array aggregate, or change the procedure to a function. I have recommended this course of action. I still think that this type of error should not occur. In many situations, the solution may not so simple. Ada's behavior should not be so "implementation dependent". - Chris