1

Question: Why do we need a return statement after all?

My ambiguity: I'm asking this because we can use the print() function instead of the return statement and call our function without printing it. If we do this, we don't get a None return value either, so why do we use a return statement at all?

More explanation: I know if we don't use the "return statement" and print our function when we call it, we get a None return value, but that's useless to me, as I'm not sure why we do that. I've experimented that we don't need to "print" our function when we call it to get the "return value", instead, we can simply replace the return statement with print() and then call our function, without printing it. With this approach, we don't get the None return value either.

This what I'm trying to convey:

def multiplication(x, y): # A function without a return statement.
    multiply = x * y
    print(multiply)       # Instead of using return statement, I used print() function.

multiplication(2, 2)      # Instead of printing the function, as if I'd have done that, I would've gotten 'None' as return value at the end; instead, I simply called the function. 
Hung
  • 155
  • 10
  • 1
    imagine you ask this question from your browser and it is printed to you and return None to SO and no one else know what you have asked, but you know what you ask as you can see it in your browser – sahasrara62 Mar 03 '23 at 13:45
  • 3
    Does this answer your question? [What is the purpose of the return statement? How is it different from printing?](https://stackoverflow.com/questions/7129285/what-is-the-purpose-of-the-return-statement-how-is-it-different-from-printing) – Talha Tayyab Mar 03 '23 at 13:51
  • In your case just printing is OK. But if you need the result of a function A to do something on your function B you will need to return something. For example if a want to multiply the result of multiplication(2, 2) by 3 your function multiplication() must return something. – nox Mar 03 '23 at 13:51
  • Because you *can't* replace `return` with `print` when what you really need is a return value. Only in the interactive interpreter is the return value of a function automatically printed to standard output. – chepner Mar 03 '23 at 13:52
  • 1
    What if you wanted to do something with the result of that function rather than printing it? By using `return` you have the flexibility to do whatever you want (including printing it, if that's what you want). In software engineering it is considered good design to have a function do only one thing ("separation of concerns"), so that you can combine functions more flexibly. – kindall Mar 03 '23 at 13:52
  • @GodIsOne Thanks, your link helped me understand the concept thoroughly. – Muhammad Tayyab Ullah Mar 03 '23 at 14:09
  • @kindall thanks for your concise yet thoughtful answer. – Muhammad Tayyab Ullah Mar 03 '23 at 14:09
  • @MuhammadTayyabUllah you're welcome brother Tayyab ! – Talha Tayyab Mar 03 '23 at 14:43

0 Answers0