SWAP command request :)

Previous topic - Next topic

ampos

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

Slydog

Hmm . . . now if we had function overloading this could be possible to do on our own . . .  ! :D
Even for TYPES!
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Ruidesco

Truth is, you now have that command.
*Directed by M. Night Shyamalan* :P

Kitty Hello

OK, I'll add a SWAP command.
Will it impact code? I mean "SWAP" might have been used as a variable in old code).

kanonet

#4
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?
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

spicypixel

#5
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
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

Moru


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 :-)

Kitty Hello

my SWAP command is a template, working for any object type.