0

i just can not understand the importance of return at the end of functions.with return u have to type print(function()) anyways so why cant we just type print(value) at the and of the function and when we need to use that function just call it like function()?

def example():
    value = 5+5
    print(value)

example()
chepner
  • 497,756
  • 71
  • 530
  • 681
vazha21
  • 13
  • 2
  • What happens when you want to do something other than print? – Mitch Apr 30 '19 at 19:47
  • You can, it just won't *do* the same thing. `print` writes to standard output; `return` sets the value of the function as an expression. Compare the value of `x` after `x = example()` using each to see the difference. – chepner Apr 30 '19 at 19:48
  • 1
    You use `print` in real code *far* less often than you seem to think you would. – chepner Apr 30 '19 at 19:48
  • Put another way, would you expect `print(example() + example())` to print 20? Why or why not? – chepner Apr 30 '19 at 19:50

3 Answers3

2

print and return are not doing the same thing. The former is displaying information to the user, while the latter is giving a raw value out of a function. Consider the following:

>>> def print_10():
...     print(10)
... 
>>> def return_10():
...     return 10
... 
>>> print(return_10() + return_10())
20
>>> print(print_10() + print_10())
Traceback (most recent call last):
  ...
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

return_10 gives back the number 10, while print_10 prints the value 10 to the console and returns None (since you don't specify a return value). Trying to use that value elsewhere is possible (None is a valid result), but it probably doesn't do what you want it to do.

Thinking of this in explicit typing makes this very clear:

def print_10() -> None:
    print(10)

def return_10() -> int:
    return 10
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

the print statement only prints what is inside the Parenthesis to the console to be seen and can't be stored in variables or do any operations on it as it is a NoneType i will show you with an example:

def example():
    a = 5
    b = 6
    print(a + b)


x = example() + 5
print(x)

the above code will return a TypeError. but if you change it to this:

def example():
    a = 5
    b = 6
    return(a + b)


x = example() + 5
print(x)

you get 16

if you want to display info and not use it again use print else use return

Amr Bashir
  • 103
  • 1
  • 2
  • 12
0

There's a principle called separation of concerns that proposes that each unit of code (function, class, module, etc.) should be responsible for doing just one thing (where the definition of "one thing" is flexible to account for the size of the unit of code being discussed). The benefit is that code that follows this principle is easier to test, debug, combine, and reuse.

If you write a function that prints its result, it is harder to reuse that code in situations where you don't want to print the result, but rather use it in some other way. The function is doing two things: calculating its result, and printing it. If you don't want it to do the second thing, or want it to do the second thing in a slightly different way, you are S.O.L. On the other hand, if the function just calculates its result, doing just one thing, the caller can easily print it if that's what they want.

That said, if you often find yourself printing a result, then it might make sense to provide a second function that calls the first and prints the result, especially if printing it is complicated. But having the first function available is a win for future code reuse.

Some development methodologies advise you to write the simplest thing that works. But I think preserving separation of concerns is an obvious and logical reason to ignore this advice in many cases.

kindall
  • 178,883
  • 35
  • 278
  • 309