How can I return a value from a function to a variable ? My function contains print statements and I need to return more than one value to more than 1 variable but I don't want the print statement to be "printed" everytime I return the value. Is that possible ?
for example : x = 4 y = 7 def switch (z,w): ***this will switch z to w and vice verca*** c= z z=w w=c print 'Now x =', w, 'and y = ' , z return w x = switch(x,y) This is an example made on the fly so there might be syntax errors but that's basically what I wanna do. How am I supposed to do so I can return also a value to the variable y WITHOUT printing 'Now x =', w, 'and y = ' , z a second time thanks in advance
>>> def return2( x, y ): ... return( y, x ) ... >>> x = 2 >>> y = 3 >>> z,k = return2( x,y ) >>> z 3 >>> k 2
oh I got it, I return a tuple then I can work on that ? I don't quiet get what you wrote tho.
you can use pointers to return multiplay values to your main program if you have study them already
a=f(x,y)
Functions are called in most languages by the name of the function and the arguments that you're passing into the function in parentheses. The parens have nothing to do with a tuple. Python is unusual allowing more than one return value. In the snippet that I posted, z and k are individual variables. If they were a tuple, I would have had to refer to them as an indexed value. In most languages, you'd have to pass a references to variables in the calling scope that the function could then set, and not "technically" return anything.
Join our real-time social learning platform and learn together with your friends!