3

I think the output should be x = 5, y = 3. But, when I tried executing it in jes it shows that x=3 and y=5.

My swap function is as follows:

def swap (x,y):    
    temp=x
    x=y
    y=temp

And from my driver function I call swap():

def driver():    
    x=3
    y=5
    swap(x,y)
    print x
    print y

I want to know why isn't the output as expected?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Amiya
  • 39
  • 3

4 Answers4

3

Well this is not a big issue in python you can return multiple values like try this snippet it might help you.

def swap(a,b):
   return (b,a)

def driver(x,y):
    print "Previous x=",x,"y=",y
    x,y = swap(x,y)
    print "Now x=", x, "y=",y

driver(3,5)
Chitrank Dixit
  • 3,961
  • 4
  • 39
  • 59
  • why my answer is downvoted, It answers the question. The person who asked question just mentioned that swap is not working so I just gave him a working snippet. – Chitrank Dixit Nov 12 '15 at 05:19
1

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.

Community
  • 1
  • 1
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
-1

Integers are immutable in python. You are setting y to x and x to y in the local scope, but are not modifying the reference to the initial integers passed in. In python, the best you can do if you want to encapsulate a swap function that modifies in-place instead of by return is pass some kind of container object that is mutable and contain references to the thing you want to swap:

def swap(container):
    container[0], container[1] = container[1], container[0]

And then call it like so:

x = 3
y = 5
container = [x,y]
swap(container)

FWIW, swap in python can be implemented in one line as simply as:

x, y = y, x

Which is probably syntactically clearer in most cases anyway.

See also Python passing an integer by reference

Community
  • 1
  • 1
lemonhead
  • 5,328
  • 1
  • 13
  • 25
-1

In python assignment changes the identity of an object rather than its value (unless you are mutating the content). This is worth noting that, as per Python Data Model

Every object has an identity, a type and a value.

So interchanging the identities inside swap would not change the identity inside the driver. Moreover, python does not allow you to change the value of an immutable types so, there is no other possible ways to swap the values inside the swap method.

Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • @Downvoter, do you have a problem understanding `Python Data Model`? – Abhijit Nov 12 '15 at 04:43
  • I'm not the down voter but I'd say this answer is difficult to understand and of little use to the OP who is clearly a beginner. – Paul Rooney Nov 12 '15 at 04:48
  • @PaulRooney: But this is a concept every beginner needs to know (vouching on the fact that I was a beginner myself). But then it is a perspective – Abhijit Nov 12 '15 at 04:50