Does SB have a SWAP function?

Post Reply
marklpenni
Posts: 1
Joined: Sun Jun 10, 2018 5:33 pm
My devices: iPhone 7+
Flag: United States of America

Does SB have a SWAP function?

Post by marklpenni »

Or how can I implement a SWAP(x,y) function in SB?
This doesn't work for me as I thought it might...

a=1 ! b=2
PRINT a,b
swap(a,b)
PRINT a,b
END

DEF swap(x,y)
temp=x
x=y
y=temp
ENDDEF


Many thanks!
Mark

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Does SB have a SWAP function?

Post by Mr. Kibernetik »

In BASIC language there is no SWAP function.

In any case you will need to use extra assignment to change variables values.

Code: Select all

a=1 ! b=2
PRINT a,b
swap(a,b)
a=swap.x ! b=swap.y
PRINT a,b
END

DEF swap(x,y)
temp=x
x=y
y=temp
ENDDEF
Or more simple way, without a function:

Code: Select all

a=1 ! b=2
PRINT a,b
temp=a ! a = b ! b = temp
PRINT a,b
END

Post Reply