Skip to content
Advertisement

how to return truth value to the “base function” in recursion?

my function looks like this:

JavaScript

here, when we return True or False, the return value of the previous recursive call is not affected.

What I want to do is: if a recursive function returns True, the “base case function” should also return True immediately

A workaround I’ve found is using a nonlocal variable:

JavaScript

but I was wondering if there was a nicer way to do the same

Advertisement

Answer

It’s just this.

JavaScript

Do use the return value of func(i) as a boolean value.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement