Olga
Olga

Reputation: 1022

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!

Upvotes: 5

Views: 396

Answers (6)

Maxime Lorant
Maxime Lorant

Reputation: 36161

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.

Upvotes: 8

dawg
dawg

Reputation: 103824

You can have a keyword argument that will be a switch of whether of not you want the result printed before returning from your function:

def func(arg1, arg2,verbose=True):
    # do cool stuff
    # 'result' is what it all produced
    if verbose:
        print result
    return result

Then just call func(arg1,arg2,...,verbose=False) to turn it off.

Upvotes: 0

Minh
Minh

Reputation: 361

No you can't. When you call a function the value return is print() (not value of result)

Upvotes: 0

Serial
Serial

Reputation: 8043

If you only care about printing it you should just do:

print(result)

at 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

you probably shouldn't do return print(result) just do either or print it outside the function

if you use return print(result) you aren't returning anything so its pointless

Upvotes: 0

poke
poke

Reputation: 387647

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

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336148

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.

Upvotes: 10

Related Questions