Home > other >  How to return the value of a swap function
How to return the value of a swap function

Time:12-13

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**?

CodePudding user response:

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])
  • Related