GLBasic forum

Main forum => GLBasic - en => Topic started by: ampos on 2011-Oct-13

Title: SWAP command request :)
Post by: ampos on 2011-Oct-13
Can we have a SWAP command? It interchange the value of 2 variables:

Code (glbasic) Select
function swap: a,b
   local temp
   temp=a
   a=b
   b=temp
endfunction


=D

Yeah, Im lazy
Title: Re: SWAP command request :)
Post by: Slydog on 2011-Oct-13
Hmm . . . now if we had function overloading this could be possible to do on our own . . .  ! :D
Even for TYPES!
Title: Re: SWAP command request :)
Post by: Ruidesco on 2011-Oct-13
Truth is, you now have that command.
*Directed by M. Night Shyamalan* :P
Title: Re: SWAP command request :)
Post by: Kitty Hello on 2011-Oct-14
OK, I'll add a SWAP command.
Will it impact code? I mean "SWAP" might have been used as a variable in old code).
Title: Re: SWAP command request :)
Post by: kanonet on 2011-Oct-15
Quote from: Kitty Hello on 2011-Oct-14OK, I'll add a SWAP command.
lol really? was sure this was just joking... A new command for something you can do with 3 lines of code? Will your command be faster than doing this by myself? Otherwise I wont see a reason for adding such a command, werent you always trying to keep the number of available commands as low as possible?
Title: Re: SWAP command request :)
Post by: spicypixel on 2011-Oct-15
Actually I think it would make for neater code, especially if you were doing card shuffling in a loop, save calling a function. Not that it would be quicker but would be neater :~)

This is cool for not using a temp variable...
Code (glbasic) Select
a% = bXOR(a%,b%) ; b% = bXOR(b%,a%) ; a% = bXOR(a%,b%)

as a function...

Code (glbasic) Select

FUNCTION Swap: BYREF SwapValue1%, BYREF SwapValue2%
SwapValue1% = bXOR(SwapValue1%,SwapValue2%)
SwapValue2% = bXOR(SwapValue2%,SwapValue1%)
SwapValue1% = bXOR(SwapValue1%,SwapValue2%)
ENDFUNCTION
Title: Re: SWAP command request :)
Post by: Moru on 2011-Oct-15

Code (glbasic) Select

FUNCTION swap: BYREF a, BYREF b
    LOCAL temp
    temp = a
    a = b
    b=temp
ENDFUNCTION


Should work, right? Having a fewer so might be thinking strange. Untested :-)
Title: Re: SWAP command request :)
Post by: Kitty Hello on 2011-Oct-18
my SWAP command is a template, working for any object type.