As other answers have suggested, this really doesn't require functions. Since you have decided to use them though, you might as well try and understand a bit more about scopes.
In order to retain the swapped values you need to return them or else they get lost after the execution of swap()
:
def swap (x,y):
temp=x
x=y
y=temp
return x, y # must return the swapped values.
Then, in the function where you call swap()
you assign the returned values to the variables that you swapped:
def driver():
x=3
y=5
x, y = swap(x,y) # re-assign values
print x
print y
Running driver()
now will give you the swapped value:
5
3
This happens because in the function swap()
the variables x
and y
you pass as arguments are treated as local to that function, only swap
can see their value. If you don't return this value back, it is lost and forgotten.