This forum is now read-only. Please use our new forums! Go to forums
What precisely does "return" in this code do?
For example, I really don’t know understand if something bad would happen if that code wasn’t there. Nor do I really understand why one puts that line of code with return on it there. Wouldn’t it work even without return?
def smallest_number(*args):
print min(args)
return min(args)
def distance_from_zero(arg):
print abs(arg)
return abs(arg)
Answer 51c1a3059c4e9dd0fb00a7a0
This is a very fundamental principle in programming, so I’ll take a minute to explain: a function can take some input (usually one or more variables or values) and return some output (usually a single value).
For instance, your smallest_number
function takes one or more arguments as input and return
s the smallest of the numbers (provided they’re all actually numbers) as output. That’s precisely what the return
keyword is for. Without it, your function returns nothing (technically, it returns an object that is used in Python to represent nothingness, namely None
).
Not every function needs to return a value. Some just don’t. If all you want is to print the number, then you don’t really need to return
it. So in the following example you won’t notice any difference:
function i_return(x):
print x
return x
function i_dont(x):
print x
i_return(5) # prints 5
i_dont(5) # prints 5
But consider another scenario, where you’d want to use the returned value (the output) inside another calculation or expression:
print i_return(5) + 2 # prints 7
print i_dont(5) + 2 # causes an Error
See, now there’s trouble. To come back to the functions you asked about, you can, for instance, use the smallest_number
in another expression, and you can only do that because of the return
keyword:
print smallest_number(9,7,2) + 8 # prints 10
Actually, if you look more closely, Python’s built-in min()
function also return
s a value. Your function is just a wrapper around it. Technically here’s what happens:
- you pass a bunch of numbers to your
smallest_number()
function - it makes a list of them and passes that list to
min()
min()
returns the smallest member of the list back to your function- your function returns that number to whoever called it (probably the top level of your Python script)
Answer 53718e25548c359ffa0002bf
Thank you Alex,I think I get the return deal now .In a earlier function lesson I was banging my head because I didn’t realize the return function basically cancelled out everything after it.Thanks for taking the time to explain.And yes,I am a complete noob.52 years old and I’m sitting here trying to learn a programming language,go figure?
2 comments
Ha! I’m 48 and have never programmed in my life. I’m banging my head through most of these lessons.
<-44. Glad I am not alone :~}
Answer 53728d2f9c4e9dbf0e00017d
2 comments
It appears that args is not the essential piece here but the *. From what I’ve read it is (args) rather than () to help understand its role.
The impressionI I got from this post: https://freepythontips.wordpress.com/2013/08/04/args-and-kwargs-in-python-explained/
Thx…very useful
Answer 5307949652f863a3f30044a6
I’ve googled this and read several explanations and this is the best one. Thanks.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
13 comments
This was very helpful, thanks!
Yes, very helpful with a decent definition of NONE as well. TYVM.
Nicely detailed explanation. One thing I don’t understand after reading this though, is why ‘return’ seems to kill code before it. For instance, if I’m defining a function, and enter a ‘return’ in the middle of it, then enter some code after it, its like the interpreter doesn’t recognize the code that follows the ‘return’ statement.
Sorry for the loose wording, but I’m a novice.
That’s exactly what return is supposed to do. If at some point in a method you say return, it’s like saying “stop whatever it is you’re doing and get outta here NOW”, so anything after the return keyword is ignored.
Thanks Alex
Thanks for the explanation Alex.
Either I’m dumb, or I just can’t understand it. You wrote what many seemed to make sense of, but I, on the other hand, understand squat about the point of return.
One of the best technical explanations I’ve seen in a while
very good explanation
seems resonable
help me a lot !
Thanks, helped me a lot, it’s very clear, LIKE
thx