# Idiom #21 Swap values

Swap the values of the variables a and b

``````#include <utility>
using std::swap;``````
``swap(a, b);``
``````procedure swap(a, b: in out Integer)
is
temp : Integer;
begin
temp := a;
a := b;
b := temp;
end swap;``````
``````a^=b;
b^=a;
a^=b;
``````
``````(defn swap [a b]
[b a])``````
``````IDENTIFICATION DIVISION.
PROGRAM-ID. swap.
PROCEDURE DIVISION.
MOVE a    TO temp
MOVE b    TO a
MOVE temp TO b
STOP RUN.``````
``````var tmp = a;
a = b;
b = tmp;``````
``(a, b) = (b, a);``
``import std.algorithm.mutation : swap;``
``swap(a,b);``
``````auto temp = a;
a = b;
b = temp;``````
``````var tmp = a;
a = b;
b = tmp;``````
``{a, b} = {b, a}``
``````fun1(A, B) ->
do:something(),
fun2(B, A).

fun2(A, B) ->
now(A, is, B),
and(B, is, A),
keep:moving().``````
``````subroutine swap(a, b)
integer, intent(inout) :: a, b
integer :: temp
temp = a
a = b
b = temp
end subroutine swap``````
``````tmp = a
a = b
b = tmp``````
``a, b = b, a``
``````swap (a,b) = (b,a)
``````
``````var tmp = a;
a = b;
b = tmp;``````
``[a, b] = [b, a];``
``````T tmp = a;
a = b;
b = tmp;``````
``a = b.also { b = a }``
``(rotatef a b)``
``a, b = b, a``
``__typeof(a) _temp=a; a=b; b=_temp;``
``````\$tmp = \$a;
\$a = \$b;
\$b = \$tmp;``````
``[ \$b, \$a ] = [ \$a, \$b ];``
``list(\$b, \$a ) = [ \$a, \$b ];``
``````tmp := a;
a := b;
b := tmp;``````
``(\$a, \$b) = (\$b, \$a);``
``a, b = b, a``
``a, b = b, a``
``````std::mem::swap(&mut a, &mut b);
``````
``let (a, b) = (b, a);``
``````val tmp = a
a = b
b = tmp

``````
``````(define (swap a b)
(list b a))
``````
``[| temp | temp := a. a := b. b := temp] value``
``````Sub Swap(Of T)(ByRef a As T, ByRef b As T)
Dim tmp as T
tmp = a
a = b
b = tmp
End Sub``````

programming-idioms.org