-1

Let me clarify; Let us say that you have 2 functions in Python:

def helloNot():
    a = print("Heya!")
helloNot()

Which will print out Heya! without a return statement.

But if we use a return statement in this function:

def hello():
    a = print("Heya!")
    return a
hello()

This will print out Heya! as well.

I have read and learned that a return statement returns the result back to the function but

  • doesn't the result get automatically returned by whatever result you have without a return statement inside a function?

In our case let's use the function helloNot() (our function without the return statement):

our variable a, which is a print statement returns the result to the function when we call it or am I missing something?

On a side note,

  • Why and when would we use return statements?

  • Is it a good habit to start using return statements?

  • Are there a lot more advantages to using return statements than there are disadvantages?

EDIT:

using the print statement was an example to better present my question. My question does NOT revolve around the print statement. Thank you.

KoyaCho
  • 155
  • 3
  • 12
  • "doesn't the result get automatically returned by whatever result you have without a return statement inside a function?" No, not at all. In Python, functions without return statements will automatically return `None`. "our variable a, which is a print statement returns the result to the function when we call it or am I missing something?" Yes. First, print is a function which returns `None`. Printing and returning are two *completely separate things*. The value of `a` will be `None` – juanpa.arrivillaga Aug 16 '18 at 23:36

4 Answers4

2

Normally, when you call a function, you want to get some result. For example, when I write s = sorted([3,2,1]), that call to sorted returns [1,2,3]. If it didn't, there wouldn't be any reason for me to ever call it.

A return statement is the way a function provides that result. There's no other way to do that, so it's not a matter of style; if your function has a useful result, you need a return statement.


In some cases, you're only calling a function for its side-effects, and there is no useful result. That's the case with print.

In Python, a function always has to have a value, even if there's nothing useful, but None is a general-purpose "no useful value" value, and leaving off a return statement means you automatically return None.

So, if your function has nothing useful to return, leave off a return statement. You could explicitly return None, but don't do that—use that when you want the reader to know you're specifically returning None as a useful value (e.g., if your function returns None on Tuesday, 3 on Friday, and 'Hello' every other day, it should use return None on Tuesdays, not nothing). When you're writing a "procedure", a function that's called only for side-effects and has no value, just don't return.


Now, let's look at your two examples:

def helloNot():
    a = print("Heya!")

This prints out Heya!, and assigns the return value of print to a local variable, which you never use, then falls off the end of the function and implicitly returns None.

def hello():
    a = print("Heya!")
    return a

This prints out Heya!, and assigns the return value of print to a local variable, and then returns that local variable.

As it happens, print always returns None, so either way, you happen to be returning None. hello is probably a little clearer: it tells the reader that we're returning the (possibly useless) return value of print.

But a better way to write this function is:

def hi():
    print("Heya!")

After all, we know that print never has anything useful to return. Even if you didn't know that, you know that you didn't have a use for whatever it might return. So, why store it, and why return it?

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 1
    I think that fundamentally the OP doesn't understand the distinction between print and return. I think this happens a lot when people learn on the REPL, which always prints the result of an evaluated expression, and more confusingly, doesn't print `None` as a special case – juanpa.arrivillaga Aug 16 '18 at 23:43
  • 1
    @juanpa.arrivillaga When I were a lad, you had to connect a teletype to an S100 and punch in the machine code for your interpreter before you could even get a REPL. And we didn't have your fancy keys to type on, we had serrated metal spikes that sawed off your fingers. And after typing your command, we had to go away to work in the mines for 25 hours before we could come home to see `??? ERROR ON LINE ??? ERROR DISPLAYING LINE NUMBER *** ***` as the output. 'Course in them days nobody could afford electricity, so we had to power our computers with a hand crank. But we were happy! – abarnert Aug 17 '18 at 00:01
  • @abarnert. This clarified things up. As an answer to your question, I have merely used the `print` statement to better explain myself and what I'm getting at. I could've used other statements that does not require a **_return_** statement (if that makes sense). @juanpa.arrivillaga ^ – KoyaCho Aug 17 '18 at 00:06
  • @KoyaCho but see, I think you *are* confused here. `print` *isn't a statement*. – juanpa.arrivillaga Aug 17 '18 at 00:18
  • @juanpa.arrivillaga actually, it _was_ a statement. It has been made into a function in Python 3.x. Thanks for the reminder. Old habits die young, like they say. – KoyaCho Aug 17 '18 at 00:30
  • @KoyaCho Sure, but when it was a statement, `a = print "Heya!"` would have just been a `SyntaxError`, so I don't think you would have asked the same question. And I think that's what juanpa is getting at here. – abarnert Aug 17 '18 at 00:40
  • You're absolutely right. As of now, I'm still struggling at the differences between Python 2 and 3. Thank you both! – KoyaCho Aug 17 '18 at 00:46
1

You should use return statements if you want to compute a value from a function and give it back to the caller.

For your example, if the goal of the function is just to print a fixed string, there's no good reason to return the return value of print.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
1

If you don't return anything from a function, Python implicitly returns a None. print falls in this category.

In [804]: a = print('something')
something

In [806]: print(a)
None

Similarly with functions that the user defines

In [807]: def f():
     ...:     print('this is f')
     ...:

In [808]: fa = f()                # Note this is assigning the *return value* of f()
this is f

In [809]: print(fa)
None
aydow
  • 3,673
  • 2
  • 23
  • 40
1

What you are doing does not require a return statement, you're right but consider you want to calculate an average.

def calculateAverage(x, y, z):
    avg = ((x + y + z)/3)
    return avg

Now that you have declared a function that has the ability to take 3 variables and return the calculated average you can now call it from any function and not have to have bulky code.

a = calculateAverage(7, 5, 9)
print("Average is:" + a)

Which will print to screen "Average is: 7"

The power of functions and return values is that you are able to make your code more readable by means of placing a single call to a sophisticated function in your main logic, which means you now have less lines of code and it is more legible/maintainable in the longrun.

Hopefully this helps.

vividpk21
  • 384
  • 1
  • 11