[Tutor] defining functions.. why return?

Kirby Urner urnerk@qwest.net
Wed, 05 Dec 2001 23:48:49 -0800


>
>THe thing is, if a function doesn't execute a return statement, it
>returns 0 when it finishes.

Not technically correct.  Unless you explicitly return a
value, a function returns None, not 0.

>def func():
>     if 0: return 1  # never runs

   >>> a = func()
   >>> a
   >>> type(a)
   <type 'NoneType'>
   >>> None == func()
   1

>This function will return 0. In the book you're reading,
>the function returns 0 explicitly for clarity. If you drop
>it, it'll make no difference.

Since None evaluates the same as 0 in an if statement,
it's true that dropping 'return 0' in the example given
will work the same.  However, this is not because the
function without a 'return 0' behaves identically.

Some other calling code might look for a return of 0,
and None wouldn't qualify.

   >>> None == 0
   0

Kirby