Python recursive function call with if statement -
i have question regarding function-calls using if-statements , recursion. bit confused because python seems jump if statements block if function returns "false"
here example:
1 def function_1(#param): 2 if function_2(#param): 3 #do 4 if x<y: 5 function_1(#different parameters) 6 if x>y: 7 function_1(#different parameters)
my function_2 returns "false" python continues code on line 5 example. can explain behavior? in advance answers.
edit: sorry, forgot brackets
concrete example:
1 def findexit(field, x, y, step): 2 if(isfieldfree(field, x, y)): 3 field[y][x] = filledmarker 4 findexit(field, x + 1, y, step+1) 5 findexit(field, x - 1, y, step+1) 6 findexit(field, x, y + 1, step+1) 7 findexit(field, x, y - 1, step+1) 8 elif(isfieldescape(field, x, y)): 9 way.append(copy.deepcopy(field)) 10 waystep.append(step+1) def isfieldfree(field, x, y): if field[y][x] == emptymarker: return true else: return false def isfieldescape(field, x, y): if field[y][x] == escapemarker: return true else: return false
after both functions "isfieldfree" , "isfieldescape" return false python continues code in line 5 in line 6.
you may misunderstand how recursion works, yes continues @ line 5 or 6 because recursion has ended @ lower level in call stack, continues @ higher-level in call stack. here's sample call stack, note next operation after false
next findexit()
@ higher call stack:
1 findexit(...): 2 true: 3 field assignment 4.1 findexit(x+1) 2 true 3 field assignment 4.1 findexit(x+1): 2 false # not jump line 5 in current call stack. 5.1 findexit(x-1): . ...
Comments
Post a Comment