Home:ALL Converter>What's the best between return the result and print it at the end of the function?

What's the best between return the result and print it at the end of the function?

Ask Time:2013-12-26T06:39:18         Author:Olga

Json Formatter

Assume we define a function and then end it with:

def function():
     # ...
     return result
# To see the result, we need to type:
print(function())

Another option is to end a function with a print:

def function():
     # ...
     print(result)

# no need of print at the call anymore so
function()

Question: May I end function with a return statement, get it via function() or not?
I mean I don't care whether function saves the result or not. However the function can have several different results, i.e I need to quit the loop at some point. The main idea is to get the output on the screen.
So, let me know please whether my variant is OK or it's not 'elegant' coding. Thanks!

Author:Olga,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/20777336/whats-the-best-between-return-the-result-and-print-it-at-the-end-of-the-functio
Minh :

No you can't. When you call a function the value return is print() (not value of result)",
2013-12-25T22:47:41
Tim Pietzcker :

If you return print(result), you're essentially doing return None because print() returns None. So that doesn't make much sense.\n\nI'd say it's much cleaner to return result and have the caller decide whether to print() it or do whatever else with it.",
2013-12-25T22:43:05
Maxime Lorant :

The most cleaner way is with the return statement. In general, a function returns a result, and this result can be processed after by another algorithm. Maybe you don't need to get the result in a variable in your case, but imagine you do in 1 week, month... \n\nThe best way is to delegate the print at the main program itself. You'll manage data in your program more easily and, as I said, you can chain functions.\n\nImagine two functions a(arg) and b(arg) that returns two calculations. With the print statement in the b function, you'll not be able to do this:\n\na(b(10))\n\n\nbecause a will receive a None value in argument (because functions returns None by default, which is the case with the print statement at the end).\n\nTL;DR: Follow this pattern most of the time:\n\ndef get_full_name(arg1, arg2, ...):\n # Do cool stuff\n return res # <- result of the function\n\n\nprint get_full_name('foo', 'bar')\nfull_name = get_full_name('Maxime', 'Lorant')\nprint some_other_function(full_name)\n# etc.\n",
2013-12-25T22:51:35
poke :

The print function will not return anything; it will only print the stuff as output to the console. So returning the return value from the print function does not really make much sense. You are returning nothing by definition.\n\nSo unless you want to end the function early, there is no reason why you should use return in this case. Just printing the result (without using return) is fine and a lot clearer.\n\nAlso, if you want to return the actual content the print function prints, then you should just do that instead and leave it to the caller to decide whether to print it or not.\n\nIf you want to use print(function()) (which to me sounds the most reasonable here), then just return the result from the function:\n\ndef function ():\n # ...\n return result\n",
2013-12-25T22:44:16
Serial :

If you only care about printing it you should just do:\n\nprint(result)\n\nat the end of the function, but generally if you are trying to use the result then just return it and then you can use the result and print it\n\nyou probably shouldn't do return print(result) just do either or print it outside the function \n\nif you use return print(result) you aren't returning anything so its pointless",
2013-12-25T22:41:52
yy