0

I have written two recursive functions in python.

The first one takes parameters, modifies them, and returns a value.

def Similarity(string, sstring1, index):
    if condition1:
        return index
    if condition2:
        #do something
    Similarity(string, string1, index)

The second one takes parameters and performs operations on a global variable.

def getData(i, value):
    global dataList
    if condition:
        return list(suffixList)
    #do something
    getData(i, value)

These functions work perfectly and do the exact computations I need but always return a NoneType.

I haven't been able to figure out why.

1
  • 1
    Functions without return smth but None or without return at all return None.
    – alexvassel
    Dec 13, 2012 at 15:47

1 Answer 1

12

You are not returning anything. Always use the return statement to return a value from a function. Python does not use the last statement in a function as a return value.

def getData(i, value):
    global dataList
    if condition:
        return list(suffixList)
    #do something
    return getData(i, value)

A function that exits without an explicit return, returns None instead.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.