Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 448
Olga Avatar asked Dec 25 '13 22:12

Olga


People also ask

Should I use print or return?

Use print when you want to show a value to a human. return is a keyword. When a return statement is reached, Python will stop the execution of the current function, sending a value out to where the function was called. Use return when you want to send a value from one point in your code to another.

What is the difference between return and print in function?

print just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. return is how a function gives back a value. This value is often unseen by the human user, but it can be used by the computer in further functions.

Should I put return at the end of a function?

It is always good to implement return in your functions so you can even specify error code for your functions for reference. So I know the code is running to last line. Otherwise the function just finishes silently.

What is the difference between return and print in Python function?

The print() function writes, i.e., "prints", a string or a number on the console. The return statement does not print out the value it returns when the function is called. It however causes the function to exit or terminate immediately, even if it is not the last statement of the function.


3 Answers

If you return print(result), you're essentially doing return None because print() returns None. So that doesn't make much sense.

I'd say it's much cleaner to return result and have the caller decide whether to print() it or do whatever else with it.

like image 183
Tim Pietzcker Avatar answered Oct 04 '22 02:10

Tim Pietzcker


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...

The 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.

Imagine 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:

a(b(10))

because 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).

TL;DR: Follow this pattern most of the time:

def get_full_name(arg1, arg2, ...):
    # Do cool stuff
    return res   # <- result of the function


print get_full_name('foo', 'bar')
full_name = get_full_name('Maxime', 'Lorant')
print some_other_function(full_name)
# etc.
like image 33
Maxime Lorant Avatar answered Oct 04 '22 03:10

Maxime Lorant


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.

So 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.

Also, 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.

If you want to use print(function()) (which to me sounds the most reasonable here), then just return the result from the function:

def function ():
    # ...
    return result
like image 42
poke Avatar answered Oct 04 '22 03:10

poke