This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by Chandler
almost 11 years

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

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)

points
Submitted by Alex J
almost 11 years

13 comments

Sharada almost 11 years

This was very helpful, thanks!

Woody Williams over 10 years

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

djangoN00b over 10 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.

Alex J over 10 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.

djangoN00b over 10 years

Thanks Alex

Thanks for the explanation Alex.

Manny4us over 9 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.

ta2tyrants over 9 years

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

nvijaysankar about 9 years

very good explanation

hill chill about 9 years

seems resonable

Ivysour about 9 years

help me a lot !

Tiger X almost 9 years

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

GangstaCoder almost 9 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?

points
Submitted by Bubba
almost 10 years

2 comments

Bridget Kokoski about 9 years

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

Rustin almost 9 years

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

Answer 53728d2f9c4e9dbf0e00017d

2 votes

Permalink

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

points
Submitted by LH1979
almost 10 years

2 comments

Jarvis over 9 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/

GangstaCoder almost 9 years

Thx…very useful

Answer 5307949652f863a3f30044a6

0 votes

Permalink

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

points
Submitted by Ignite Mindz
about 10 years