Jump to content
Sign in to follow this  
FreedomFry

Recursion Error

Recommended Posts

FreedomFry

I have the same stack issue with autoit closing as a few others here have encountered. I know what I did wrong. I have:

Func1

If...Func2

Else...Func3

EndFunc

Func3

If...Func4

Else...Func1

EndFunc

So basically, I check things and go back to step 1 if the functions don't find their values. It ends up creating functions inside functions...etc. Instead of pointing back at step one, how can I FINISH and go BACK to step 1? The actual code is much more complicated, however if you can solve this, I can easily apply it. I want to avoid a massive logic nest, as that would be tedious and error prone in my case.

Thanks

Share this post


Link to post
Share on other sites
happyuser

The model of recursivity is computing of function fac.

Here is a example of programming fac with recursivity and it works.

#include <constants.au3>
#include <GUIConstantsEx.au3>
GUICreate("My GUI Button",300,100); will create a dialog box that when displayed is centered
    $label = GUICtrlCreateLabel("0.00",10,10,200)
    $pg=GUICtrlCreateProgress(10,30,200,-1,0x01)
    $Button_1 = GUICtrlCreateButton("Run .EXE", 10, 50, 100)
    GUISetState()    ; will display an  dialog box with 2 button
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $Button_1
                $f=fac(10)
        guictrlsetdata($label,$f)
        EndSelect
    WEnd
    
Func fac($i)
    if $i=1 then 
        return 1
    Else
        return $i*fac($i-1)
    EndIf
EndFunc

What you can see is that there is a test that insures that recursivity is not infinite. $i decreases and at last, when it is equal to 1 the recursivity stops. You have to insure that in your case, this is true.

You could code your problem like that :

func 1

if cond 1 then

res 1

else if cond 2 then

res 2

else func 1

endfunc

That sort of recursivity works providing that it is never, in any case, infinite.

Regards

Share this post


Link to post
Share on other sites
FreedomFry

You mean trying something like this:

Infinite Loop

Select

Case 1

Func1

If...Func2

Else...Func3

Case 2

Func3

If...Func4

Else...Func1

EndSelect

Can I omit the second else statement and it will automatically go back to Select again?

Share this post


Link to post
Share on other sites
happyuser

No, I mean :

func recursiv_func($x)
if condition1 then 
   return func2
else
   if condition2 then
       return func3
   else return recursiv_func(phi($x))
   endif
endif

where phi($x) insures that the algorithm converges for example phi($x)=$x-1 and recursiv_func(1) is a known constant.

See example of fac(x) where fac is called recursively except when x=1 for which fac(x)=1.

Edited by happyuser

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×