473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

return keyword

6
In a Python function definition, sometimes you don't see the return keyword at all. Sometimes, you see it all by itself. And sometimes, you see it associated with a bunch of variables - like "return x, y, z".

Can someone clearly explain the differences between these 3 scenarios? What exactly is happening? What is the Python keyword "return" for?
Feb 28 '08 #1
5 3736
jlm699
314 100+
In a Python function definition, sometimes you don't see the return keyword at all. Sometimes, you see it all by itself. And sometimes, you see it associated with a bunch of variables - like "return x, y, z".

Can someone clearly explain the differences between these 3 scenarios? What exactly is happening? What is the Python keyword "return" for?
Let's say you have the following funtion:
Expand|Select|Wrap|Line Numbers
  1. def xfive(inp):
  2.     tmp = 5
  3.     result = tmp * inp
  4.     return result
  5.  
  6. # Call the function like this (>>> means this was done from the shell)    
  7. >>> xfive(3)
  8. 15
  9.  
This function takes an input, and returns a result... although it doesn't have to take an input... it could just as easily be the following:
Expand|Select|Wrap|Line Numbers
  1. def threexfive():
  2.     tmp = 5
  3.     result = tmp * 3
  4.     return result
  5.  
  6. # Then you would call the function like this
  7. >>> threexfive()
  8. 15
  9.  
When a function doesn't have the return statement at the end, it still "returns" to the calling function, however it does no return a value.

When using a mutli-variable return such as:
Expand|Select|Wrap|Line Numbers
  1. def ret3(inp):
  2.     tmp = 5
  3.     tmp2 = 2
  4.     result = tmp * inp
  5.     return result, tmp2, tmp
  6.  
  7. # Call the function and receive three elements back
  8. >>> mylist = xfive(3)
  9. >>> mylist
  10. [15, 2, 5]
  11.  
Basically, you can return any number of elements that you would like.

Does that clear it up a little bit? I suggest referring to the :Python documentation if you would like more explanation of basic programming techniques. You can find it here: http://www.python.org/doc/

Oh, and btw... if you see simply return with no variables associated it's the same thing as not having a return statement at all. It's just a matter of preference.
Feb 28 '08 #2
Expand|Select|Wrap|Line Numbers
  1. def func1():
  2.     return
  3.  
  4. def func2():
  5.     return 5,6,7
  6. def func3():
  7.     print 5
  8.  
  9. if __name__ == '__main__':
  10.     a = func1()
  11.     b = func2()
  12.     c = func3()
  13.  
  14. print type(a), a
  15. print type(b), b
  16. print type(c), c
  17.  
  18.  
Above code has the 3 scenarios you have mentioned.

func3 does not have a return statement
func1 has a return statement by itself
func2 has a return statement with 3 integers returned

In python, function returns one and only object.

In the above code the returned objects are stored in the variable a, b & c.

Be default any function returns "NoneType" object which is the case for func3 & func1

In case of func2, it returns "Tuple" object. return 3,4,5 is equivalent to return (3,4,5).

SKN
Feb 28 '08 #3
jlm699
314 100+
Additionally:
Expand|Select|Wrap|Line Numbers
  1. >>> a, b, c = func2()
  2. >>> a
  3. 5
  4. >>> b
  5. 6
  6. >>> c
  7. 7
  8.  
You could assign each element of the tuple to its own variable without needing to bother with storing the entire object
Feb 28 '08 #4
pyn00b
6
Does that clear it up a little bit? I suggest referring to the :Python documentation if you would like more explanation of basic programming techniques. You can find it here: http://www.python.org/doc/

Oh, and btw... if you see simply return with no variables associated it's the same thing as not having a return statement at all. It's just a matter of preference.
Thank you! Yes, this is clearer to me now. So no return statement means that no value is returned (after reading elsewhere a little I found this means it returns a value of None). Anything more than 1 variable being returned results in a list being returned. As well, I didn't know that no return and a return without a variable are the exact same thing.
Feb 29 '08 #5
bvdet
2,851 Expert Mod 2GB
..snip.. Anything more than 1 variable being returned results in a list being returned. ..snip..
Actually a tuple is returned. Tuples are similar to lists but have very distinct differences.
Feb 29 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
2
by: sugaray | last post by:
i was just told in the 'simple base-conveter program' thread, that using 'return' is not portable in such case: if(argc!=2) { fprintf("error message\n"); return 1; } it should be replace by...
1
by: Steven.Xu | last post by:
Hi every one. I have two class: MyBase and MySon. MyBase is the base class of MySon. On the other hand I have two interface: MyBaseItef and MySonItef. MyBaseItef is the base interface of...
2
by: Mike | last post by:
I keep running into the scenario below over and over again. Currently I get around not having covariant return types by using an interface and explicit property definitions which works to some...
2
by: Raj | last post by:
Hi, I was getting an error in my program saying that return keyword must not be followed by an object expression. I was confused first because I was using return statement in a valid "if"...
19
by: Chantelle | last post by:
I've got this A2K DB that has a continuous form that lists Suppliers and their details. The form has a field for each supplier that holds several Keywords that reflect the suppliers products or...
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
9
by: Samuel Shulman | last post by:
Hi I wander there is a way to return ByRef just like passing ByRef What I want to achieve is the following: Call a method that returns an object Use that call as an argument to a ByRef...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.