Generic swap: Difference between revisions

m (→‎{{header|Wren}}: Now uses List.swap method.)
 
(17 intermediate revisions by 14 users not shown)
Line 432:
a 1 ng
</pre>
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="applesoft basic">A=43:B=47:H=A:A=B:B=H:?" A="A" B="B;</syntaxhighlight>
<pre> A=47 B=43</pre>
 
=={{header|Axe}}==
Line 440 ⟶ 437:
<syntaxhighlight lang="axe">Exch(°A,°B,2)</syntaxhighlight>
 
=={{header|Batch FileBASIC}}==
==={{header|Applesoft BASIC}}===
 
<syntaxhighlight lang="applesoft basic">A=43:B=47:H=A:A=B:B=H:?" A="A" B="B;</syntaxhighlight>
Swap using pass-by-name
<pre> A=47 B=43</pre>
 
<syntaxhighlight lang="dos">@echo off
setlocal enabledelayedexpansion
set a=1
set b=woof
echo %a%
echo %b%
call :swap a b
echo %a%
echo %b%
goto :eof
 
:swap
set temp1=!%1!
set temp2=!%2!
set %1=%temp2%
set %2=%temp1%
goto :eof</syntaxhighlight>
 
 
 
=={{header|BaCon}}==
 
==={{header|BaCon}}===
<syntaxhighlight lang="freebasic">
x = 1
Line 474 ⟶ 451:
</syntaxhighlight>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">global a, b
a = "one"
Line 488 ⟶ 465:
end subroutine</syntaxhighlight>
 
==={{header|BBC BASIC}}===
====Built-in function====
<syntaxhighlight lang="bbcbasic"> a = 1.23 : b = 4.56
SWAP a,b
Line 497 ⟶ 474:
SWAP a$,b$
PRINT a$,b$</syntaxhighlight>
====Custom function====
<syntaxhighlight lang="bbcbasic"> a = 1.23 : b = 4.56
PROCswap(^a,^b, 5)
Line 518 ⟶ 495:
world! Hello
</pre>
 
==={{header|Chipmunk Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|FreeBASIC}}===
FreeBASIC already has a built-in generic Swap procedure but a macro can be used to build another one:
<syntaxhighlight lang="freebasic">' FB 1.05.0
#Macro Declare_Swap(T)
Sub Swap_##T(ByRef t1 As T, ByRef t2 As T)
Dim temp As T = t2
t2 = t1
t1 = temp
End Sub
#EndMacro
 
Dim As Integer i, j
i = 1 : j = 2
 
Declare_Swap(Integer) ' expands the macro
Swap_Integer(i, j)
Print i, j
 
Dim As String s, t
s = "Hello" : t = "World"
 
Declare_Swap(String)
Swap_String(s, t)
Print s, t
 
Print
Print "Press any key to exit"
Sleep</syntaxhighlight>
 
{{out}}
<pre>
2 1
World Hello
</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">window 1, @"Generic Swap", (0,0,480,270)
 
text ,,,,, 60
 
long i, j
double x, y
CFStringRef a, b
 
i = 1059 : j = 62
print i, j
swap i, j
print i, j
 
print
 
x = 1.23 : y = 4.56
print x, y
swap x, y
print x, y
 
print
 
a = @"Hello" : b = @"World!"
print a, b
swap a, b
print a, b
 
HandleEvents
</syntaxhighlight>
 
{{output}}
<pre>
1059 62
62 1059
 
1.23 4.56
4.56 1.23
 
Hello World!
World! Hello
</pre>
 
==={{header|Gambas}}===
'''[https://gambas-playground.proko.eu/?gist=b746e857f6f280fb92c204795f6053be Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim vA As Variant = " World"
Dim vB As Variant = 1
 
Swap vA, vB
 
Print vA; vB
 
End</syntaxhighlight>
Output:
<pre>
1 World
</pre>
 
==={{header|GW-BASIC}}===
{{works with|Applesoft BASIC}}
{{works with|Chipmunk Basic}}
{{works with|MSX_BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 CLS : REM 10 HOME for Applesoft BASIC
20 A = 1 : B = 2
30 PRINT " A=";A," B=";B
40 T = A : A = B : B = T
50 PRINT " A=";A," B=";B
60 END</syntaxhighlight>
{{out}}
<pre> A=1 B=2
A=2 B=1</pre>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 DEF SWAP(REF A,REF B)
110 LET T=A:LET A=B:LET B=T
120 END DEF
130 LET A=1:LET B=2
140 PRINT A,B
150 CALL SWAP(A,B)
160 PRINT A,B</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{works with|QBasic}}
{{works with|QuickBasic}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|IS-BASIC}}
{{works with|MSX_Basic}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="qbasic">10 LET A = 1
20 LET B = 2
30 PRINT " A=";A;" B=";B
40 LET T = A
50 LET A = B
60 LET B = T
70 PRINT " A=";A;" B=";B
80 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|OxygenBasic}}===
<syntaxhighlight lang="text">
macro Swap(a,b, c)
typeof(a) c
c=a
a=b
b=c
end macro
 
 
'demo with compound types:
'=========================
type point { float x,y}
point p={1,2}
point q={3,4}
swap p,q
print "p: " p.x "," p.y 'p: 3,4
print "q: " q.x "," q.y 'q: 1,2
</syntaxhighlight>
 
==={{header|PureBasic}}===
Built in function:
<syntaxhighlight lang="purebasic">Swap a, b</syntaxhighlight>
 
==={{header|QBasic}}===
QBasic already has a generic swap procedure built in, but a new subroutine can be defined:
<syntaxhighlight lang="qbasic">SUB nswap (a, b)
temp = a: a = b: b = temp
END SUB
 
a = 1
b = 2
 
PRINT a, b
CALL nswap(a, b)
PRINT a, b</syntaxhighlight>
 
==={{header|Quite BASIC}}===
The [[#Minimal _BASIC|Minimal BASIC]] solution works without any changes.
 
==={{header|Run BASIC}}===
Run BASIC does not have support for swapping built in:
<syntaxhighlight lang="runbasic">a = 1
b = 2
'----- swap ----
tmp = a
a = b
b = tmp
end</syntaxhighlight>
 
==={{header|ThinBASIC}}===
Generic function, swap the content of two variables.
<syntaxhighlight lang="thinbasic">Swap Var1, Var2</syntaxhighlight>
 
==={{header|TI-89 BASIC}}===
TI-89 BASIC is dynamically typed, so the genericity is implicit. It has no pass by reference, so we must pass the variable names as strings. It is dynamically scoped, so we must choose hopefully distinct names for the variables.
 
<syntaxhighlight lang="ti89b">Define swap(swapvar1, swapvar2) = Prgm
Local swaptmp
#swapvar1 → swaptmp
#swapvar2 → #swapvar1
swaptmp → #swapvar2
EndPrgm
 
1 → x
2 → y
swap("x", "y")
x
2
y
1</syntaxhighlight>
 
==={{Header|Tiny BASIC}}===
<syntaxhighlight lang="tiny basic"> LET a = 11
LET b = 22
PRINT a, " ", b
GOSUB 100
PRINT a, " ", b
END
100 REM swap(a, b)
LET t = a
LET a = b
LET b = t
RETURN</syntaxhighlight>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">SUB swap(a, b)
LET temp = a
LET a = b
LET b = temp
END SUB
 
LET a = 1
LET b = 2
 
PRINT a, b
CALL swap(a, b)
PRINT a, b
END</syntaxhighlight>
 
==={{header|uBasic/4tH}}===
Since uBasic/4tH has a stack (just like [[Forth]]) and it is an integer BASIC only, this is quite trivial. However, making a function or procedure with the same functionality is impossible, because there is no way to pass variables by reference.
<syntaxhighlight lang="text">a = 5 : b = 7
Print a,b
Push a,b : a = Pop() : b = Pop()
Print a,b</syntaxhighlight>
 
==={{header|VBScript}}===
This works for everything: strings, dates, booleans ... The fact is, with everything being a Variant, it's always generic.
 
<syntaxhighlight lang="vb">sub swap( byref x, byref y )
dim temp
temp = x
x = y
y = temp
end sub</syntaxhighlight>
 
Usage:
<syntaxhighlight lang="vb">dim a
a = "woof"
dim b
b = now()
swap a,b
wscript.echo a
wscript.echo b</syntaxhighlight>
 
{{out}}
<syntaxhighlight lang="vb">5/02/2010 2:35:36 PM
woof</syntaxhighlight>
 
==={{header|Visual Basic}}===
Visual Basic can use the [[#VBScript|VBScript]] example above, with the caveat that it won't work if any <code>DEFtype</code> (except <code>DefVar</code>) has been used. (The default data type is <code>Variant</code>, which can be used as a stand-in for any variable type.)
 
Also, the sub will fail if one arg is a string containing non-numeric data and the other arg is numeric.
 
==={{header|Visual Basic .NET}}===
Semantically identical to [[#C#|C#]]
<syntaxhighlight lang="vbnet">Sub Swap(Of T)(ByRef a As T, ByRef b As T)
Dim temp = a
a = b
b = temp
End Sub</syntaxhighlight>
 
Usage:
<syntaxhighlight lang="vbnet">Dim a = 1, b = 2
Swap(a, b)</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
XBasic already has a generic swap procedure built in, but a new subroutine can be defined:
<syntaxhighlight lang="qbasic">
PROGRAM "nswap"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
a = 1
b = 2
 
PRINT a, b
GOSUB nswap
PRINT a, b
 
SUB nswap
temp = a: a = b: b = temp
END SUB
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
Yabasic already has a generic swap procedure built in.
<syntaxhighlight lang="yabasic">a = 1
b = 2
 
print a, b
//----- swap ----
temp = a : a = b : b = temp
print a, b</syntaxhighlight>
 
=={{header|Batch File}}==
 
Swap using pass-by-name
 
<syntaxhighlight lang="dos">@echo off
setlocal enabledelayedexpansion
set a=1
set b=woof
echo %a%
echo %b%
call :swap a b
echo %a%
echo %b%
goto :eof
 
:swap
set temp1=!%1!
set temp2=!%2!
set %1=%temp2%
set %2=%temp1%
goto :eof</syntaxhighlight>
 
=={{header|Beads}}==
Line 537 ⟶ 863:
B = [1, 2, 3, 4, 5]
</pre>
 
=={{header|Binary Lambda Calculus}}==
From https://github.com/tromp/AIT/blob/master/rosetta/swap.lam we get the 25-bit swap function
<pre>0001100000000101101101110</pre>
 
=={{header|BQN}}==
Line 1,091 ⟶ 1,421:
(swap (car lst) (cadr lst))
;; now lst is '(456 123)</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun swap = Pair by var a, var b do return var%var(b => a) end
int a = 1
int b = 2
writeLine("before swap: a=" + a + ", b=" + b)
Pair pair = swap(a, b)
a = pair[0]
b = pair[1]
writeLine(" after swap: a=" + a + ", b=" + b)
</syntaxhighlight>
{{out}}
<pre>
before swap: a=1, b=2
after swap: a=2, b=1
</pre>
 
=={{header|Erlang}}==
Line 1,236 ⟶ 1,583:
Test me
me Test</pre>
 
=={{header|FreeBASIC}}==
FreeBASIC already has a built-in generic Swap procedure but a macro can be used to build another one:
<syntaxhighlight lang="freebasic">' FB 1.05.0
#Macro Declare_Swap(T)
Sub Swap_##T(ByRef t1 As T, ByRef t2 As T)
Dim temp As T = t2
t2 = t1
t1 = temp
End Sub
#EndMacro
 
Dim As Integer i, j
i = 1 : j = 2
 
Declare_Swap(Integer) ' expands the macro
Swap_Integer(i, j)
Print i, j
 
Dim As String s, t
s = "Hello" : t = "World"
 
Declare_Swap(String)
Swap_String(s, t)
Print s, t
 
Print
Print "Press any key to exit"
Sleep</syntaxhighlight>
 
{{out}}
<pre>
2 1
World Hello
</pre>
 
=={{header|Frink}}==
Line 1,279 ⟶ 1,591:
</syntaxhighlight>
 
=={{header|FutureBasicFōrmulæ}}==
<syntaxhighlight lang="futurebasic">window 1, @"Generic Swap", (0,0,480,270)
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Generic_swap}}
text ,,,,, 60
 
'''Solution'''
long i, j
double x, y
CFStringRef a, b
 
Fōrmulæ supports assignments of several symbols (provided as a list) from an expression that reduces to a list of the same cardinality (the expression is first reduced before the actual assignment).
i = 1059 : j = 62
print i, j
swap i, j
print i, j
 
This can be used to do a generic swap as follows:
print
 
[[File:Fōrmulæ - Generic swap 01.png]]
x = 1.23 : y = 4.56
print x, y
swap x, y
print x, y
 
'''Test case'''
print
 
[[File:Fōrmulæ - Generic swap 02.png]]
a = @"Hello" : b = @"World!"
print a, b
swap a, b
print a, b
 
[[File:Fōrmulæ - Generic swap 03.png]]
HandleEvents
</syntaxhighlight>
 
{{output}}
<pre>
1059 62
62 1059
 
1.23 4.56
4.56 1.23
 
Hello World!
World! Hello
</pre>
 
=={{header|Fōrmulæ}}==
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
In '''[https://formulae.org/?example=Generic_swap this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=b746e857f6f280fb92c204795f6053be Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim vA As Variant = " World"
Dim vB As Variant = 1
 
Swap vA, vB
 
Print vA; vB
 
End</syntaxhighlight>
Output:
<pre>
1 World
</pre>
 
=={{header|Gecho}}==
Line 1,573 ⟶ 1,836:
b = temporary(c)
end</syntaxhighlight>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 DEF SWAP(REF A,REF B)
110 LET T=A:LET A=B:LET B=T
120 END DEF
130 LET A=1:LET B=2
140 PRINT A,B
150 CALL SWAP(A,B)
160 PRINT A,B</syntaxhighlight>
 
=={{header|J}}==
Line 1,613 ⟶ 1,867:
V2
cat</syntaxhighlight>
 
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
fn swap<T>(anon a: &mut T, anon b: &mut T) {
let temporary = *a
*a = *b
*b = temporary
}
 
fn main() {
mut a = "Hello"
mut b = "World"
 
println("{} {}", a, b)
swap(&mut a, &mut b)
println("{} {}", a, b)
 
mut c = 1
mut d = 2
 
println("{} {}", c, d)
swap(&mut c, &mut d)
println("{} {}", c, d)
}
</syntaxhighlight>
 
=={{header|Java}}==
Line 1,760 ⟶ 2,039:
-> (new world) (hello brave)
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
# Swap function with call-by-pointer
fp.swap = ($[aPtr], $[bPtr]) -> {
$tmp = $*aPtr
$*aPtr = $*bPtr
$*bPtr = $tmp
}
 
$a = 42
$b = A short text
 
fn.println($a, $b)
 
fp.swap($a, $b)
 
fn.println($a, $b)
</syntaxhighlight>
 
{{out}}
<pre>
42, A short text
A short text, 42
</pre>
 
=={{header|Lang5}}==
Line 1,766 ⟶ 2,070:
 
=={{header|langur}}==
Langur does not have an option for using references (so far). The following is not directly applicable to the task, but valuesValues can be swapped using multi-variable assignment, including indexed values (for mutable variables).
 
{{works with|langur|0.10}}
<syntaxhighlight lang="langur">var .abc = [1, 2, 3]
var .def = [5, 6, 7]
Line 1,776 ⟶ 2,079:
writeln .abc
writeln .def</syntaxhighlight>
 
Prior to 0.10, you would use parentheses as follows.
 
<syntaxhighlight lang="langur">(.abc[3], .def[3]) = (.def[3], .abc[3])</syntaxhighlight>
 
{{out}}
Line 1,923 ⟶ 2,222:
 
<syntaxhighlight lang="m2000 interpreter">
\\ pgrammingprogramming again Swap (for local use)
Module Swap (&a, &b) {
\\ this call internal command - by default is by reference without using character &
Line 2,185 ⟶ 2,484:
swap(locals, "x", "y")
print "AFTER: x=" + x + ", y=" + y</syntaxhighlight>
{{out}}
<pre>BEFORE: x=1, y=2
AFTER: x=2, y=1</pre>
Line 2,306 ⟶ 2,605:
 
<syntaxhighlight lang="oforth">swap</syntaxhighlight>
 
=={{header|OxygenBasic}}==
 
<syntaxhighlight lang="text">
macro Swap(a,b, c)
typeof(a) c
c=a
a=b
b=c
end macro
 
 
'demo with compound types:
'=========================
type point { float x,y}
point p={1,2}
point q={3,4}
swap p,q
print "p: " p.x "," p.y 'p: 3,4
print "q: " q.x "," q.y 'q: 1,2
</syntaxhighlight>
 
=={{header|Oz}}==
Line 2,730 ⟶ 3,008:
Y = 1.
</syntaxhighlight>
 
=={{header|PureBasic}}==
Built in function:
<syntaxhighlight lang="purebasic">Swap a, b</syntaxhighlight>
 
=={{header|Python}}==
Line 2,746 ⟶ 3,020:
return b, a</syntaxhighlight>
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|QBasic}}==
QBasic already has a generic swap procedure built in, but a new subroutine can be defined:
<syntaxhighlight lang="qbasic">SUB nswap (a, b)
temp = a: a = b: b = temp
END SUB
 
a = 1
b = 2
 
PRINT a, b
CALL nswap(a, b)
PRINT a, b</syntaxhighlight>
 
=={{header|Quackery}}==
Line 2,925 ⟶ 3,186:
1
</syntaxhighlight>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! Code
! Comments
|-
|
≪ OVER TYPE 6 == OVER TYPE 6 == 2 * + 1 +
{ ≪ SWAP ≫
≪ OVER EVAL SWAP ROT STO ≫
≪ DUP EVAL ROT ROT STO ≫
≪ OVER EVAL OVER EVAL 4 ROLL STO SWAP STO ≫ }
SWAP GET EVAL
≫ 'GSWAP' STO
1 2 GSWAP
'A' 'B' GSWAP
3 'A' GSWAP
|
":#" means "stack level #"
calculate case index upon types of data at :1 and :2
case 1: swap :2 with :1
case 2: swap content of variable named at :2 with :1
case 3: swap content of variable named at :1 with :2
case 4: swap content of variables named at :1 and :2
select program from case index and execute it
|}
 
=={{header|Ruby}}==
Line 2,945 ⟶ 3,240:
puts x # prints string
puts y # prints 42</syntaxhighlight>
 
=={{header|Run BASIC}}==
 
Run BASIC does not have support for swapping built in:
<syntaxhighlight lang="runbasic">a = 1
b = 2
'----- swap ----
tmp = a
a = b
b = tmp
end</syntaxhighlight>
 
=={{header|Rust}}==
Line 3,057 ⟶ 3,341:
; (2 1)</syntaxhighlight>
 
=={{header|Seed7sed}}==
There are no variables in ''sed''. Just a "pattern space" and a "hold space" — that's all. The <code>x</code> command exchanges the contents of both.
 
=={{header|Seed7}}==
A generic template to generate swap functions is defined with:
<syntaxhighlight lang="seed7">const proc: generate_swap (in type: aType) is func
Line 3,229 ⟶ 3,515:
<pre>before a=1 b=2
after a=2 b=1</pre>
 
=={{header|ThinBASIC}}==
Generic function, swap the content of two variables.
<syntaxhighlight lang="thinbasic">Swap Var1, Var2</syntaxhighlight>
 
=={{header|TI-89 BASIC}}==
 
TI-89 BASIC is dynamically typed, so the genericity is implicit. It has no pass by reference, so we must pass the variable names as strings. It is dynamically scoped, so we must choose hopefully distinct names for the variables.
 
<syntaxhighlight lang="ti89b">Define swap(swapvar1, swapvar2) = Prgm
Local swaptmp
#swapvar1 → swaptmp
#swapvar2 → #swapvar1
swaptmp → #swapvar2
EndPrgm
 
1 → x
2 → y
swap("x", "y")
x
2
y
1</syntaxhighlight>
 
=={{Header|Tiny BASIC}}==
<syntaxhighlight lang="tiny basic"> LET a = 11
LET b = 22
PRINT a, " ", b
GOSUB 100
PRINT a, " ", b
END
100 REM swap(a, b)
LET t = a
LET a = b
LET b = t
RETURN</syntaxhighlight>
 
=={{header|Trith}}==
As with other stack-based languages (e.g. [[Factor]] and [[Joy]]), the solution to this task is a trivial matter of swapping the top two operands on the stack:
<syntaxhighlight lang="trith">swap</syntaxhighlight>
 
=={{header|True BASIC}}==
<syntaxhighlight lang="qbasic">SUB swap(a, b)
LET temp = a
LET a = b
LET b = temp
END SUB
 
LET a = 1
LET b = 2
 
PRINT a, b
CALL swap(a, b)
PRINT a, b
END</syntaxhighlight>
 
=={{header|TXR}}==
Line 3,327 ⟶ 3,561:
 
<code>with-update-expander</code> is a macro which writes code for accessing and updating a place, and makes that code available as local macros. The result is wrapped around the body of code passed to the macro; the body can access these functions, using a backquote to insert the symbols which refer to them. For instance the macro call <code>(,l-getter)</code> expands to code which accesses the prior value of the <code>left</code> place, and <code>(,r-setter ,tmp)</code> stores the value of the temporary variable into the <code>right</code> place.
 
=={{header|uBasic/4tH}}==
Since uBasic/4tH has a stack (just like [[Forth]]) and it is an integer BASIC only, this is quite trivial. However, making a function or procedure with the same functionality is impossible, because there is no way to pass variables by reference.
<syntaxhighlight lang="text">a = 5 : b = 7
Print a,b
Push a,b : a = Pop() : b = Pop()
Print a,b</syntaxhighlight>
 
=={{header|UNIX Shell}}==
Line 3,385 ⟶ 3,612:
='hi' 'hello'
 
=={{header|VBScript}}==
This works for everything: strings, dates, booleans ... The fact is, with everything being a Variant, it's always generic.
 
<syntaxhighlight lang="vb">sub swap( byref x, byref y )
dim temp
temp = x
x = y
y = temp
end sub</syntaxhighlight>
 
Usage:
<syntaxhighlight lang="vb">dim a
a = "woof"
dim b
b = now()
swap a,b
wscript.echo a
wscript.echo b</syntaxhighlight>
 
{{out}}
<syntaxhighlight lang="vb">5/02/2010 2:35:36 PM
woof</syntaxhighlight>
 
=={{header|Verbexx}}==
Line 3,429 ⟶ 3,634:
 
@SAY "a=" a " b=" b;</syntaxhighlight>
 
=={{header|Visual Basic}}==
 
Visual Basic can use the [[#VBScript|VBScript]] example above, with the caveat that it won't work if any <code>DEFtype</code> (except <code>DefVar</code>) has been used. (The default data type is <code>Variant</code>, which can be used as a stand-in for any variable type.)
 
Also, the sub will fail if one arg is a string containing non-numeric data and the other arg is numeric.
 
=={{header|Visual Basic .NET}}==
Semantically identical to [[#C#|C#]]
<syntaxhighlight lang="vbnet">Sub Swap(Of T)(ByRef a As T, ByRef b As T)
Dim temp = a
a = b
b = temp
End Sub</syntaxhighlight>
 
Usage:
<syntaxhighlight lang="vbnet">Dim a = 1, b = 2
Swap(a, b)</syntaxhighlight>
 
Line 3,493 ⟶ 3,680:
 
Both approaches are illustrated below.
<syntaxhighlight lang="ecmascriptwren">var swap = Fn.new { |l| l.swap(0, 1) }
 
var a = 6
Line 3,598 ⟶ 3,785:
4.00000 3.00000
</pre>
 
=={{header|Yabasic}}==
Yabasic already has a generic swap procedure built in.
<syntaxhighlight lang="yabasic">a = 1
b = 2
 
print a, b
//----- swap ----
temp = a : a = b : b = temp
print a, b</syntaxhighlight>
 
=={{header|Yorick}}==
885

edits