So if you call:
then the values of x and y are passed to the swapNos method - not the variables themselves. Inside the method, a and b are two new variables. When you swap the contents of a and b, nothing happens to the original variables x and y in the method that you called swapNos from.
David Newton wrote:Not the way you're going about it, no, and you can't return multiple values. You can return an array and destructure, or pass in an object and change its internal values, or use the object you already have, or do it inline, or use an inline xor swap, or...
Or... just put those three lines of code in the place where you want to swap the two integer variables.
David Newton wrote:... or use an inline xor swap, or...
According to me, swapping variables with the XOR trick is only a way to show off how clever you are It looks obscure, makes your code less readable, and does not have any real advantages (for example, it's not "faster" in general because you don't need a temp variable). Don't do the XOR trick when writing real software (unless there is a really, really special reason, for example when you're programming a microcontroller in assembly language, and you really need to spare a register).