Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return the value of a swap function

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading