Feb-11-2021, 05:00 AM
(This post was last modified: Feb-11-2021, 05:00 AM by deanhystad.)
This is your code solving for n = 6, but instead of recursively calling convert() it calls a different version of convert for each value of n. So convert6() calls convert2() calls conver1() which calls convert0().
So while it is true that return will jump you out of a function, it does not jump you out of 9 levels of function calls. Return jumps you back to the function's caller, and execution resumes at the next line.
def convert0(): return new def convert1(): new.append(2 % 3 - 1) convert0() def convert2(): new.append(3 % 3 - 1) convert1() So while you are correct that return will jump you out of a function, it will not jump you back out of 9 levels of function calls. It only jumps you back to the function's caller, where execution resumes with the next command. def convert6(): new.append(7 % 3 - 1) convert2()Is it now clear why convert() is returning None? convert6() does not have a return statement, and when you call convert(6), it doesn't have a return statement either. Just because convert() returns "new" when n == 0, this does not mean that value is returned by all the other calls to convert(). That is not how function calls work. Each function call will return a value or not return a value based on the code IN THE FUNCTION. Your function convert() will return a value if the argument n == 0, otherwise it does not return a value.
So while it is true that return will jump you out of a function, it does not jump you out of 9 levels of function calls. Return jumps you back to the function's caller, and execution resumes at the next line.