From help-octave-request at bevo dot che dot wisc dot edu Fri Oct 31 19:54:15 2003 Subject: Re: Realtime cost of call by value From: "John W. Eaton" To: taltman at lbl dot gov cc: help-octave Date: Fri, 31 Oct 2003 19:53:10 -0600 On 31-Oct-2003, John W. Eaton wrote: | On 31-Oct-2003, taltman at lbl dot gov wrote: | | | This would allow for both to live side-by-side, harmoniously ( I | | believe ). ;-) | | Not even close. [...] After rereading this message, I think it may have come across as a bit rude, which was not my intent. I would be interested in a way to pass arguments by reference, but it must not be the default. Here is a (half-baked) proposal to think about. Similar to C++, I think the notion of a reference argument should be a property of the function definition, not the code that calls the function. So we would write something like function F (reference A) ... endfunction or perhaps, if you prefer concise and cryptic function F (&A) ... endfunction to declare that A is passed by reference for all calls to F rather than something like function F (A) ... endfunction F (X) F (reference (X)) to say that X is passed by reference for just some calls and not others. Given the above definition of F, you could expect F (X); # OK, pass X by reference. If F changes A, X also changes. F (1); # ERROR, reference to constant. F (X+1); # ERROR, reference to temporary. F (X(1)); # ERROR, reference to temporary. If you write Y = X; F (X); and F changes A, only X changes, not Y. Given the current implementation of Octave, a copy will be forced at the point of the function call, but not at the Y = X statement (that just increments an internal reference count). I'm not sure what syntax we should use, but a unary & as above would be one possibility that would not be incompatible with Matlab syntax (at least currently). We could also introduce a way to make one variable a reference to another apart from argument lists. Something like reference A = B; or perhaps &A = B; would cause A and B to share the same value in memory. If A changes, so does B, or if B changes, so does A. If you did something like C = B; reference A = B; then changed A, only A and B would change, not C. Again, making this work will require some additional reference counting trickery in Octave as currently the first assignment doesn't actually force a copy, it just increments an internal reference count. So in the above, we would actually force a copy at the point of the reference declaration. But that is just an implementation detail. What I'm most interested in now is what the syntax and semantics should be, not precisely how it will have to be implemented. Comments? jwe ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------