[Tutor] proper return style

Tim Peters tim.one@home.com
Sun, 28 Oct 2001 23:43:55 -0500


[Timothy Wilson]
> What's the rule about using a explicit return command in functions? For
> example:
>
> def printSquare(number):
>     print number**2
>     return
>
> works the same as:
>
> def printSquare(number):
>     print number**2
>
> What's preferred?

My rule is to leave off the "return" unless the function is intended to
return an explicit result.  So

def printSquare(number):
    print number**2

def square(number):
    return number**2

Note that if a function intends to return None as an explict value, then an
explict

    return None

is much better than the equivalent

    return

or falling off the end.  "Functions" used solely for side-effects (such as
your printSquare) are clearest if they don't use "return" at all.