Newbie question about function return values

Chad Netzer cnetzer at mail.arc.nasa.gov
Thu Mar 13 19:53:08 EST 2003


On Thu, 2003-03-13 at 16:25, Brian Munroe wrote:
> Ok, so it is late in the day and I, for the life of me, cannot figure out
> what is going on in the code below.

If you don't explicitly return a value from a function, the None object
is returned.  You didn't explicity return the value in the first part of
the if statement.  Try:

def sectors(x):
    if ((x % 512) != 0):
        x = x + 1
        return sectors(x)
    else:
        return x


BTW - There are better methods of finding the next larger power of two. 
Try starting with the value 1 and doubling it until it is larger than
the comparison value, for example.  It'll generally require less
comparisons.  Also, you do not need recursion in this case, and should
probably avoid it (you are not partioning the problem space very much
with your recursion, and which is where recursion is most helpful. 
Instead, you are using it to perform iteration, which is a bad idea in
almost all languages that provide loops).

-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)







More information about the Python-list mailing list