recursive function returns wrong result

sarkis

Member
Licensed User
Longtime User
Hi all,

I call a recursive function that returns true or false.
Even when enable =0 and Find_Way returns true (I checked on debugger)
I get a messagebox with FALSE.
Actually I can make a loop but why there is an error?

If Find_Way Then
Msgbox("","TRUE" )
Else
Msgbox("","FALSE" )
End If

Sub Find_Way As Boolean
If enable=0 Then
Return True
Else If Availables Then
Next_Move
Find_Way
Else If No_Way Then
Make_Zero
Return False
Else
Find_Way
End If
End Sub
 
Last edited:

sarkis

Member
Licensed User
Longtime User
error in recursion

Hi Erel,
thanks for responding.
Here is the project zip at attachment.
I changed the recursion to the loop and it works fine.
I keep also old function.
You can change the comment for calling old function.
If Find_Way_Old Then
to
If Find_Way Then
in function Skizb_Way.
 
Upvote 0

sarkis

Member
Licensed User
Longtime User
Ok, but field enable cannot be 0 from beginning,
it take a value in Activity_Create.
If enable=0 then there is no any recursive calls and why it works.
(in this case we have empty list)
I think there is an another reason according to recursive call,
because loop works fine.
 
Upvote 0

Jack Cole

Well-Known Member
Licensed User
Longtime User
Maybe needs to be like this? I changed the recursive call to find_way to return the result (that way the you don't lose the value of find_way in the recursive call).

B4X:
Sub Find_Way As Boolean
If enable=0 Then 
Return True
Else If Availables Then 
Next_Move
Return Find_Way
Else If No_Way Then 
Make_Zero
Return False   
Else 
Return Find_Way
End If
End Sub
 
Upvote 0

sarkis

Member
Licensed User
Longtime User
Thanks Jack Cole,
Your solution is right and function works (although I have changed it for a loop )
So what I understand from this ,that we cannot make a recursive call as we do usually.(Why?)
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
So what I understand from this ,that we cannot make a recursive call as we do usually.(Why?)

What is usually?

I may miss something but your code wouldn't work in any programming language I know without the changes Jack Cole did.
 
Upvote 0
Top