What Precisely Does "return" In This Code Do? - Codecademy

Skip to ContentProfile image of ParrhesiaSubmitted by Parrhesiaover 12 yearsWhat 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

83 votes

Permalink

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 returns 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 returns a value. Your function is just a wrapper around it. Technically here’s what happens:

  1. you pass a bunch of numbers to your smallest_number() function
  2. it makes a list of them and passes that list to min()
  3. min() returns the smallest member of the list back to your function
  4. your function returns that number to whoever called it (probably the top level of your Python script)

Profile image of fanaugenSubmitted by fanaugenover 12 years

13 comments

Profile image of SharadaNSubmitted by SharadaNover 12 years

This was very helpful, thanks!

Profile image of MrYescaSubmitted by MrYescaover 12 years

Yes, very helpful with a decent definition of NONE as well. TYVM.

Profile image of djangoN00bSubmitted by djangoN00bover 12 years

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.

Profile image of fanaugenSubmitted by fanaugenabout 12 years

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.

Profile image of djangoN00bSubmitted by djangoN00babout 12 years

Thanks Alex

Profile image of anonymousSubmitted by anonymousover 11 years

Thanks for the explanation Alex.

Profile image of Manny4usSubmitted by Manny4usabout 11 years

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.

Profile image of ta2tyrantsSubmitted by ta2tyrantsabout 11 years

One of the best technical explanations I’ve seen in a while

Profile image of nvijaysankarSubmitted by nvijaysankarabout 11 years

very good explanation

Profile image of pyRunner24467Submitted by pyRunner24467almost 11 years

seems resonable

Profile image of IvysourSubmitted by Ivysouralmost 11 years

help me a lot !

Profile image of TigSuarezSubmitted by TigSuarezover 10 years

Thanks, helped me a lot, it’s very clear, LIKE

Profile image of GangstaCoderSubmitted by GangstaCoderover 10 years

thx

Answer 53718e25548c359ffa0002bf

3 votes

Permalink

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?

Profile image of HackYoMammaSubmitted by HackYoMammaalmost 12 years

2 comments

Profile image of designSlayer27707Submitted by designSlayer27707about 11 years

Ha! I’m 48 and have never programmed in my life. I’m banging my head through most of these lessons.

Profile image of rbmunro71Submitted by rbmunro71almost 11 years

<-44. Glad I am not alone :~}

Answer 53728d2f9c4e9dbf0e00017d

2 votes

Permalink

why is it (*args) and not (args)?

Profile image of LH1979Submitted by LH1979almost 12 years

2 comments

Profile image of FUTUREJARVSubmitted by FUTUREJARVabout 11 years

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/

Profile image of GangstaCoderSubmitted by GangstaCoderover 10 years

Thx…very useful

Answer 5307949652f863a3f30044a6

0 votes

Permalink

I’ve googled this and read several explanations and this is the best one. Thanks.

Profile image of objectRockstar57999Submitted by objectRockstar57999almost 12 years

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.
    • Checker DenseBeginner Friendly.4 Lessons
  • Free course

    Learn JavaScript

    Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Checker DenseBeginner Friendly.11 Lessons
  • Free course

    Learn HTML

    Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.
    • Checker DenseBeginner Friendly.6 Lessons
Explore full catalog

Tag » What Does Return Mean In Coding