0

I was wondering if there is (or is not) a reason to call return after a function is done but no value is expected to be returned. Is there a best practice or does it just not matter?

For example:

def print_something(arg):
    print(arg)
    return

The last instruction return of function print_something. Should it be there or should it not? Does it just add unessecary instructions to executed application? Or is there just no point in thinking about it?

ap0
  • 1,083
  • 1
  • 12
  • 37
  • 1
    I think you should start with some basics: http://stackoverflow.com/questions/721090/what-is-the-difference-between-a-function-and-a-procedure – konart Nov 26 '15 at 15:11
  • Advice noted. I just use "function" for everything and never thought about the naming. – ap0 Nov 26 '15 at 15:14
  • In your exampe it does not make sense. But it would, if you for exampe have a condition which was met and you don't want to execute the rest of the function – synthomat Nov 26 '15 at 15:16
  • @ap0 agreed, there is a distinction formally but they are also equivalent in python since there is `None`. – simonzack Nov 26 '15 at 15:16

1 Answers1

1

If a function intends to perform some action and not return anything then it is customary to not include the return statement, even as None is returned:

def print_something(arg):
    print(arg)

or if return is required

def print_something(arg):
    if arg == 1:
        return
    print(arg)

However it is better practice to write out return None when a function can return None in some cases and other types in others as it is clearer what's going on:

def increment(arg):
    if arg is None:
        return None  # instead of just return
    return arg + 1
simonzack
  • 19,729
  • 13
  • 73
  • 118