Page 1 of 1

Does SB have a SWAP function?

Posted: Sun Nov 05, 2023 4:48 am
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

Re: Does SB have a SWAP function?

Posted: Sun Nov 05, 2023 4:08 pm
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