Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 8 Online
OpenStudy (anonymous):

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 ?

OpenStudy (anonymous):

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

OpenStudy (rsmith6559):

>>> def return2( x, y ): ... return( y, x ) ... >>> x = 2 >>> y = 3 >>> z,k = return2( x,y ) >>> z 3 >>> k 2

OpenStudy (anonymous):

oh I got it, I return a tuple then I can work on that ? I don't quiet get what you wrote tho.

OpenStudy (anonymous):

you can use pointers to return multiplay values to your main program if you have study them already

OpenStudy (anonymous):

a=f(x,y)

OpenStudy (rsmith6559):

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.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!