How to return the value of a swap function

Question:

I want to ask if i can return more than one value using function.
when i make a function that is not necessarly returning one value like the swap functin,I’m not sure how to deal with it.Because in the end i want to return the two variables, let us consider them x and y

To explain more i tried to do this little function:

Function Swap(x:integer,y:integer)
Variables
temp:integer
Begin
temp=x
x=y
y=temp
return x,y

the thing that I’m not sure about is if i have the right to** return more than one value**?

Asked By: saad elmoraghi

||

Answers:

You could just try it:

def some_func():
  return 4, "five", [6]

a, b, c = some_func()
print(f"{a}, {b}, {c}")
>>> 4, five, [6]

Yes, you can return as many values from a func as you like. Python will pack all the return values into a single var, a tuple, and allows you to unpack them into multiple vars when the function returns. So, you could also do:

  d = some_func()
  print(type(d))
  >>> <class 'tuple'>
  print(d)
  >>> (4, 'five', [6])
Answered By: Danielle M.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.