Question

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 havent been able to figure out why.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top