0

I wanted to write a recursive function to fit curves. For some reasons my results didn't looked good so I boiled down the problem to one easy example:

Number = 6
Successor = 0

def Fuction(Number, Successor):
    if (Number - Successor)+1>0:
       Successor += 1
       Fuction(Number, Successor)
    else:
        return(Fuction(Number, Successor))


A = Fuction(Number, Successor) 
print(f'The Successor of {Number} is {A}')

When I do this the Kernel dies. How can I fix it? (I followed the awnser : Why does my recursive function return none)

5
  • "The kernel dies" -- yikes! Are you sure this isn't just the CPython interpreter gently raising an error? What output are you expecting here and why use recursion at all? Commented Jun 2, 2021 at 15:17
  • Why are you calling Fuction() again in the if (Number - Successor) + 1 > 0 branch? When do you expect this function to stop recurring, and what is it supposed to return in that branch? Commented Jun 2, 2021 at 15:17
  • I expect this function to stop recurring if "Successor = Number + 1". Again this is an abstract version of my real problem. I just want to know how I can use the return function such that the output reads: "The Successor of 6 is 7" Commented Jun 2, 2021 at 15:21
  • I'm afraid to ask, but why not do 6 + 1? The problem here should be pretty clear by stepping with a debugger or adding a print. Your base case is supposed to be the else branch. "Base case" means "stop the recursion", yet you do return(Fuction(Number, Successor)) in this branch, continuing the recursion infinitely. You can just return Successor, then your problem reduces to the link you provided because the recursive branch isn't returning anything. As an aside, only classes should start with uppercase letters. The convention is to use lower-case for vars. Commented Jun 2, 2021 at 15:25
  • Again: This is an abstract version of my real problem. I want to create a recursive function to fit data (without using lmfit whatsoever). I want my output not just to be something I print but something I can use in my code later. When I return successor, I get the awnser 'none' when it's in the else block, or the awnser 'the successor of 6 is 1' Commented Jun 2, 2021 at 15:40

2 Answers 2

1

Maybe something like this?

Number = 6
Successor = 0

def Fuction(Number, Successor):
    if (Number - Successor) + 1 == 0: # base case for interrupting recursion
        return Successor
    Successor += 1
    return Fuction(Number, Successor)

A = Fuction(Number, Successor) # will be 7
print(f'The Successor of {Number} is {A}')

You can also do it like this which is more similar to your code example:

def Fuction(Number, Successor):
    if (Number - Successor) + 1 > 0:
        Successor += 1
        return Fuction(Number, Successor)
    return Successor
Sign up to request clarification or add additional context in comments.

Comments

0

The problem here is that the recursion does not terminate. The issue occurs when Number=6 and Successor=7, Fuction(6,7) just calls Fuction(6,7) again and again until the interpreter crashes due its recursion limit being exceeded. I believe this is when your kernel dies.

The solution here is to find the right base case for the recursion where it can return without making another recursive call.

Here's a nice video explaining how to implement recursion.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.