<div dir="ltr">The reason I am interested in using call by reference is that I am accessing data frames with over a million rows and hundreds of columns. It is more efficient to operate on such a data frame directly, as opposed to copying it into and out of a function. In other words, I want to be *not* like R, which is why I am interested in utilizing C++, which supports call by reference.</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jul 29, 2015 at 12:37 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
On 29 July 2015 at 07:29, Rguy wrote:<br>
| Thanks for the insight. For other readers possibly struggling with call by<br>
| reference, the following code works as expected, i.e., x is changed to c(1, 2).<br>
<br>
Nobody is struggling, but your example is still "unusual" (to avoid the<br>
loaded term "wrong") as we generally prefer functions which compute something<br>
to return something.<br>
<br>
But if you insist on side-effects, this is simpler:<br>
<br>
R> cppFunction("void myabs(NumericVector & x) { x = abs(x); }")<br>
R> x <- c(-2.0, 4.0)<br>
R> myabs(x)<br>
>R x<br>
[1] 2 4<br>
R><br>
<br>
But you should really do<br>
<br>
R> cppFunction("NumericVector myabs(NumericVector & x) { return abs(x); }")<br>
R> x <- c(-2.0, 4.0)<br>
R> myabs(x)<br>
[1] 2 4<br>
R><br>
<br>
which more like R.<br>
<span class="HOEnZb"><font color="#888888"><br>
Dirk<br>
<br>
--<br>
<a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
</font></span></blockquote></div><br></div>