Oxygen Basic
Welcome,
Guest
. Please
login
or
register
.
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News:
Latest OxygenBasic.zip at GitHub
(Click on the Wizard)
Home
Help
Search
Login
Register
Oxygen Basic
»
Programming
»
Example Code
»
Swap Function
« previous
next »
Print
Pages: [
1
]
Author
Topic: Swap Function (Read 847 times)
0 Members and 1 Guest are viewing this topic.
Nicola
Guest
Swap Function
«
on:
January 02, 2021, 09:02:50 AM »
Hello,
i was seeing and trying some swap functions (value swapping between two variables).
The attached listing gives an example and use.
Code: OxygenBasic
'Swap functions
'hello from Nicola
'-----------------
use console
sub
pprint(
string
s,a,b)
'serve per stampare i dati
printl "Using " + s +" "+a + " "+ b
end
sub
function
swap(
int
*a,
int
*b)
local
int
c=a
a=b : b=c
end
function
macro
swapp(a,b)
'funziona per qualsiasi tipo di dato
typeof
a v=a
a=b : b=v
end
macro
macro
swaps(a,b)
'funziona x qualsiasi dato.
dim
as
string
v(2)={a,b}
a=v(2) : b=v(1)
end
macro
macro
swapar(a,b)
'swap usando il + e il -
a=a+b
b=a-b
a=a-b
end
macro
macro
swaper(a,b)
'swap usando il * e il /
a=a*b
b=a/b
a=a/b
end
macro
macro
swapxor(a,b)
'swap usando il * e il / funziona per soli int
a=a
xor
b
b=a
xor
b
a=a
xor
b
end
macro
int
a=20, b=35
pprint("Before ",a,b)
swap(a,b)
pprint("swap ",a,b)
swapar(a,b)
pprint("swapar ",a,b)
swaper(a,b)
pprint("swaper ",a,b)
swapxor(a,b)
pprint("swapxor ",a,b)
swaps(a,b)
pprint("swaps ",a,b)
float
a=1.5,b=2.5
pprint("Before ",a,b)
swapp(a,b)
pprint("swapp ",a,b)
swapar(a,b)
pprint("swapar ",a,b)
swaper(a,b)
pprint("swaper ",a,b)
'swapxor(a,b) 'non funziona per float
'pprint("swapxor ",a,b)
swaps(a,b)
pprint("swaps ",a,b)
string
h="Nicola", k="Charles"
swapp(h,k)
pprint("swapp ",h,k)
swaps(h,k)
pprint("swaps ",h,k)
waitkey
«
Last Edit: January 07, 2021, 12:23:40 PM by Nicola
»
Logged
Charles Pegge
Guest
Re: Swap Function
«
Reply #1 on:
January 07, 2021, 04:08:54 AM »
Thanks Nicola.
The
swap
macro will cover most situations efficiently.
Macros often have significant advantages over procedures/functions, supporting multiple types and avoiding the overhead of making calls.
Logged
Print
Pages: [
1
]
« previous
next »
Oxygen Basic
»
Programming
»
Example Code
»
Swap Function