Search code examples
pythonfunctionreturn

How to use returned value from a function


I have a function that finds all paths through a graph. The function returns a list of all paths. How do I use this value later in my code?

def findpaths(attachednodes,startID,endID,path = []):
    path = path + [startID]

    if startID == endID:
        return [path]

    if startID not in attachednodes:
        return []

    paths = []
    for n in attachednodes[startID]:
        if n not in path:
            newpath = findpaths(attachednodes,n,endID,path)
            for new in newpath:
                paths.append(new)

    for i in range(len(paths)):
        numflight = i
        flight = paths[i]
        flights.update({numflight: flight})

    return paths

Solution

  • you put the function call to the right side of a variable assignment. The variable will have the return value: e.g.

    def some_function():
      return 10
    
    x = some_function()
    print(x) # will print 10