i was doing some exercises outside codecademy and came across this code:
def is_even(x): if x == 0: return True else: return is_odd(x-1) def is_odd(x): return not is_even(x) print(is_odd(17)) print(is_even(23))
this is the explanation:
Recursion can also be indirect. One function can call a second, which calls the first, which calls the second, and so on. This can occur with any number of functions.
but i can't wrap my head around it, can someone explain why this code works?
i understand of course that there are far more efficient ways to determine if the number is even or odd (by using
%), but i just want to understand recursive function calling each other
this is the code for the question:
def fib(x): if x == 0 or x == 1: return 1 else: return fib(x-1) + fib(x-2) print(fib(4))
but given i don't understand this, i have no idea how this could be 5.
could someone explain the example (odd/even) and/or the question?