1

How can I write results into the passed parameters (arguments)?

### permutation function

def xchg(a, b):
    """ xchg function that does a permution for two integers
    """
    a=a+b
    b=a-b
    a=a-b

a=int(input("Enter a number: "))
b=int(input("Enter another number: "))
xchg(a,b)
print("a= ", a)
print("b= ", b)
4
  • You've got the right idea, but your a and b variables change scope due to the function. Therefore a and b switch only within the scope of xchg. You'd have to return a value and assign that value to a variable if you wanted this to work. For future information, this has to do with mutability of datatypes in python. Commented Jan 26, 2013 at 18:56
  • I have preserved the actual question in my edit. It is understandable that you want to describe where you're coming from, but we can see that you're new to Python. But even if we couldn't, it doesn't change the goal that you want to achieve, which is why the information is irrelevant. Commented Jan 26, 2013 at 18:57
  • I think you misunderstood the question. "Where you're coming from" in this case is metaphorical - phant0m really means "describing your motivation and background". Commented Jan 26, 2013 at 19:59
  • @phant0m sorry for misunderstand Commented Jan 26, 2013 at 20:24

3 Answers 3

8

If you're learning Python, you should learn how to write Python, not learn how to write C or Java in Python.

In Python, there is no point in doing this. Much better:

b, a = a, b
Sign up to request clarification or add additional context in comments.

1 Comment

i know this, i just want to write a function exemple
2

You should know that what you're trying to do is not possible.

In Python, there are values and there are names (variables).

An assignment makes a name refer to a value. It follows, that the same value can have multiple names.

Function parameters are names, that are local to that function. Assigning to them cannot make a different name magically refer to a different value. All it does is "bind" the local name to a different value.

Now, there are mutable values, and there are immutable values. Remember, different names can refer to the same value. If you change the value, rather than bind a name to a different value, all other names that are bound to that value will see the change, too.

4 Comments

i think this how should be: ### permutation function def xchg(a, b): """ xchg function that does a permution for two integers """ a=a+b b=a-b a=a-b return a, b a=int(input("Enter a number: ")) b=int(input("Enter another number: ")) a,b=xchg(a,b) print("a= ", a) print("b= ", b)
def xchg(a, b): return b, a In Pyhton there is not need for the C/assembler style im-place swap pattern!
@Ber Is that directed at me or he OP?
@phant0m That was at no one in particular, just pointing out a simpler version.
0

Try:

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

and then later:

a, b = xchg(a, b)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.