Generic swap: Difference between revisions

(→‎{{header|MATLAB}} / {{header|Octave}}: merge Octave and Matlab code; the Matlab version is clearly the more elegant and efficient version, so the Octave version is removed.)
Line 1,039:
return b, a</lang>
Note that tuples are immutable in Python. This function doesn't mutate anything, but simply returns a new pair with the order of the elements switched.
 
=={{header|R}}==
 
R function arguments are passed by value, not by reference. You can work around this, however, by using their names and environment:
 
<lang R>swap <- function(name1, name2, envir = parent.env(environment()))
{
temp <- get(name1, pos = envir)
assign(name1, get(name2, pos = envir), pos = envir)
assign(name2, temp, pos = envir)
}</lang>
 
Usage:
<pre>> x <- 1
> y <- 2
> swap('x', 'y')
> cat(x, y)
2 1</pre>
 
=={{header|REBOL}}==
Anonymous user