Jump to content
Bishop12

Recursion Overflow

Recommended Posts

Hi Guys can i ask im doing something on my scripts then i have Recursion i know i call function with it self without returning to first state yes i understand it while reading autoit last day now so i want to ask Recursion can solve with this kind of  function Example below can i fix Recursion with that kind script? the reason is i want to run the script for long hours i hope i am right now  Thankyou i nadvance NEWBIE only hehe  .. i know it can be fix but im curious on Return to function im not that much  familiar in auoit i dont know if Recursion can break if using RETURN + Function or Return to first function

 

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 250, 97, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 0, 0, 100, 57)
$Button2 = GUICtrlCreateButton("Button2", 150, 30, 100, 57)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $counter = 0
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Global $Returner
            Working1()
        Case $Button2
    EndSwitch
WEnd




Func Working1()
    MsgBox(0,"","Step1")
    sleep(1000)
    Working2()
    MsgBox(0,"","STEP3")
    return Working1()
EndFunc



Func Working2()

    if $counter = 1 Then
    sleep(1000)
    MsgBox(0,"","STEP2")
    working3()
    Return
    ElseIf $counter = 0 Then
    $counter = $counter+1
    working2()
    EndIf
EndFunc


Func working3()
        sleep(1000)
    MsgBox(0,"","Working3")
    Return
EndFunc
Edited by Bishop12

Share this post


Link to post
Share on other sites

You have to master recursion in order to use it efficiently.  Your working1 function is obviously deficient, it is calling itself endlessly.  If you want to benefit from recursion, the underlying algorithm must follow a precise path, which is not clear in your example. 

Share this post


Link to post
Share on other sites
2 hours ago, Nine said:

You have to master recursion in order to use it efficiently.  Your working1 function is obviously deficient, it is calling itself endlessly.  If you want to benefit from recursion, the underlying algorithm must follow a precise path, which is not clear in your example. 

@Nine yes sir i probably need to master it but can you give me example or can you correct my Example on how can i do it so i can understand it clearly on how to fix recursion simple explanation or example would be apreciated so much

 

Share this post


Link to post
Share on other sites
6 hours ago, Bishop12 said:

Do you think making While loop in Case before Function call and return to while loop and call function again in while loop can be ?

 

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 250, 97, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 0, 0, 100, 57)
$Button2 = GUICtrlCreateButton("Button2", 150, 30, 100, 57)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $counter = 0
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
        while true
            Working1()
         WEnd
        Case $Button2
    EndSwitch
WEnd




Func Working1()
    MsgBox(0,"","Step1")
    sleep(1000)
    Working2()
    MsgBox(0,"","STEP3")
    return
EndFunc



Func Working2()

    if $counter = 1 Then
    sleep(1000)
    MsgBox(0,"","STEP2")
    working3()
    Return
    ElseIf $counter = 0 Then
    $counter = $counter+1
    working2()
    EndIf
EndFunc


Func working3()
        sleep(1000)
    MsgBox(0,"","Working3")
    Return
EndFunc

 

 

Share this post


Link to post
Share on other sites

You are still calling   working2() inside of func working2() so obviously this could cause issues.
What exactly are you expecting to happen there? (also try keeping you script readable by formatting it properly! Tidy can help there. ;) )

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 250, 97, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 0, 0, 100, 57)
$Button2 = GUICtrlCreateButton("Button2", 150, 30, 100, 57)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $counter = 0
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            While True
                Working1()
            WEnd
        Case $Button2
    EndSwitch
WEnd




Func Working1()
    MsgBox(0, "", "Step1")
    Sleep(1000)
    Working2()
    MsgBox(0, "", "STEP3")
    Return
EndFunc   ;==>Working1



Func Working2()
    If $counter = 1 Then
        Sleep(1000)
        MsgBox(0, "", "STEP2")
        working3()
        Return
    ElseIf $counter = 0 Then
        $counter = $counter + 1
        Working2() ; <=== this is a problem
    EndIf
EndFunc   ;==>Working2


Func working3()
    Sleep(1000)
    MsgBox(0, "", "Working3")
    Return
EndFunc   ;==>working3

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
1 hour ago, Jos said:

You are still calling   working2() inside of func working2() so obviously this could cause issues.
What exactly are you expecting to happen there? (also try keeping you script readable by formatting it properly! Tidy can help there. ;) )

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 250, 97, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 0, 0, 100, 57)
$Button2 = GUICtrlCreateButton("Button2", 150, 30, 100, 57)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $counter = 0
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            While True
                Working1()
            WEnd
        Case $Button2
    EndSwitch
WEnd




Func Working1()
    MsgBox(0, "", "Step1")
    Sleep(1000)
    Working2()
    MsgBox(0, "", "STEP3")
    Return
EndFunc   ;==>Working1



Func Working2()
    If $counter = 1 Then
        Sleep(1000)
        MsgBox(0, "", "STEP2")
        working3()
        Return
    ElseIf $counter = 0 Then
        $counter = $counter + 1
        Working2() ; <=== this is a problem
    EndIf
EndFunc   ;==>Working2


Func working3()
    Sleep(1000)
    MsgBox(0, "", "Working3")
    Return
EndFunc   ;==>working3

Jos 

ohh so @Jos what should i do to that script to make it infinite ? thanks for advice using Tidy Noted i learn 1 more new thing again hehe  

Share this post


Link to post
Share on other sites

Ya your working2 function is deficient too.  Not because it will cause an infinite loop, but it does nothing else than calling working3.  If the counter = 0 then it calls itself (once) and after counter reach 1 it calls working3.  The recursion there is totally useless.  So remove the counter and recursion, just call working3...

Share this post


Link to post
Share on other sites
53 minutes ago, Nine said:

Ya your working2 function is deficient too.  Not because it will cause an infinite loop, but it does nothing else than calling working3.  If the counter = 0 then it calls itself (once) and after counter reach 1 it calls working3.  The recursion there is totally useless.  So remove the counter and recursion, just call working3...

@Nine so sir can i call the function like this 

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 250, 97, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 0, 0, 100, 57)
$Button2 = GUICtrlCreateButton("Button2", 150, 30, 100, 57)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $counter = 0
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            While True
                Working1()
            WEnd
        Case $Button2
    EndSwitch
WEnd




Func Working1()
    MsgBox(0, "", "Step1")
    Sleep(1000)
    Working2()
    MsgBox(0, "", "STEP3")
    Return
EndFunc   ;==>Working1



Func Working2()
    If $counter = 1 Then
        Sleep(1000)
        MsgBox(0, "", "STEP2")
        working3()
        Return
    ElseIf $counter = 0 Then
        $counter = $counter + 1
        return Working2() ; <===  Test resolve
    EndIf
EndFunc   ;==>Working2


Func working3()
    Sleep(1000)
    MsgBox(0, "", "Working3")
    Return
EndFunc   ;==>working3

 

Share this post


Link to post
Share on other sites
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 250, 97, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 0, 0, 100, 57)
$Button2 = GUICtrlCreateButton("Button2", 150, 30, 100, 57)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $counter = 0
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Working1()
    EndSwitch
WEnd

Func Working1()
    Sleep(100)
    Working2()
EndFunc   ;==>Working1

Func Working2()
    If $counter < 3 Then
        ;you code...
        working3($counter)
        $counter += 1
        Working2()
    Else
        $counter = 0
        MsgBox(0, "", "counter Reset")
    EndIf
EndFunc   ;==>Working2


Func working3($oCounter)
    MsgBox(0, "", $oCounter & ": Working3")
EndFunc   ;==>working3

 

Edited by jugador

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...