[Rcpp-devel] Call by reference
Dirk Eddelbuettel
edd at debian.org
Wed Jul 29 13:37:50 CEST 2015
On 29 July 2015 at 07:29, Rguy wrote:
| Thanks for the insight. For other readers possibly struggling with call by
| reference, the following code works as expected, i.e., x is changed to c(1, 2).
Nobody is struggling, but your example is still "unusual" (to avoid the
loaded term "wrong") as we generally prefer functions which compute something
to return something.
But if you insist on side-effects, this is simpler:
R> cppFunction("void myabs(NumericVector & x) { x = abs(x); }")
R> x <- c(-2.0, 4.0)
R> myabs(x)
>R x
[1] 2 4
R>
But you should really do
R> cppFunction("NumericVector myabs(NumericVector & x) { return abs(x); }")
R> x <- c(-2.0, 4.0)
R> myabs(x)
[1] 2 4
R>
which more like R.
Dirk
--
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
More information about the Rcpp-devel
mailing list