Swapping using Functions in Swift

Swapping two variables in Swift using Functions

Source code :


func swap(a: Int ,b: Int) -> (Int,Int){

return (b,a)
    
}

var a: Int = 10  , b: Int = 20

print("Before swapping", a, b)

var s = swap(a,b:b)

print("After swapping ", s.0, s.1 )

Output :

Before swapping 10 20
After swapping  20 10

Notes :

Here we have swapped the two numbers by using Functions.


Output :


                

Comments :