Jump to content

Restart function from start?


Recommended Posts

Hi,

I'm wondering if there is a way I can restart a function from within itself?

I mean if a certain condition is meet in an If/EndIf can I tell the script to go back to the begining of the fuction and start again? or maybe pop out of it and recall it?

My AutoIt Scripts.- AutoHost and Password Enabler for Delta Force 2 Demo.| Caffine for Winamp 2.9x and WRS 2.0 | mp3 directory cleaner | CRAP DJ | A:B:J Radio Automation Software | FFMPEG batch conversion automator

Link to comment
Share on other sites

While 1
; Start of function
; Restart if X
    If X Then ContinueLoop
; End of function
    ExitLoop/Return (Value)
WEnd
this works if it's a loop, but for an actual function, it wouldn't. You are able to call a function from within itself, that's recursion. one thing to remember though, is that when the called function returns, the current instance will still complete. so if you want to just start the function over based on a condition, i'd do something like:

$condition = 1
MyFunc()
Func MyFunc()
MsgBox(0,"title","Function called")
If $condition then
$condition = 0
MyFunc()
Exit
endif
MsgBox(0,"title","Function completed")
EndFunc

the "Exit" after the function calling itself makes sure that when the called instance ends, and control returns to the original instance, nothing is done by that instance. that's why you get 2 "function called" messages, and only one "function completed" message.

***edit*** extra words...blah

Edited by cameronsdad
Link to comment
Share on other sites

cameronsdad, one small problem with your approach is that recursion is still involved and could therefore potentially cause a stack overflow.

Restarting a function should not actually involve the function calling itself. My approach does indeed work for an actual function because under normal circumstances it would not loop; the loop is only there to provide the restarting point. Here's a more practical example to try out:

Func GetAddrLabel()
    Local Const $Title = 'Getting Address Label Details'
    Local Const $Restart = @LF & "(Enter 'restart' to restart)"
    While 1
        Local $Name = InputBox($Title, 'Your name:' & $Restart)
        If @Error Then Return ''
        If $Name = 'restart' Then ContinueLoop
        Local $Address = InputBox($Title, 'Your street number and name:' & $Restart)
        If @Error Then Return ''
        If $Address = 'restart' Then ContinueLoop
        Local $Suburb = InputBox($Title, 'Your suburb:' & $Restart)
        If @Error Then Return ''
        If $Suburb = 'restart' Then ContinueLoop
        Local $State = InputBox($Title, 'Your state:' & $Restart)
        If @Error Then Return ''
        If $State = 'restart' Then ContinueLoop
        Local $Postcode = InputBox($Title, 'Your postcode:' & $Restart)
        If @Error Then Return ''
        If $Postcode = 'restart' Then ContinueLoop
        Local $Country = InputBox($Title, 'Your country:' & $Restart)
        If @Error Then Return ''
        If $Country = 'restart' Then ContinueLoop
        Local $Output = $Name & @LF & $Address & @LF
        $Output = $Output & $Suburb & ' ' & $State & ' ' & $Postcode & @LF
        $Output = $Output & $Country
        Return $Output
    WEnd
EndFunc

MsgBox(0x40, 'Send to', GetAddrLabel())

This example has many points where the function should restart. Since there is no real code outside the While..WEnd loop, the entire function is essentially being restarted.

Link to comment
Share on other sites

cameronsdad, one small problem with your approach is that recursion is still involved and could therefore potentially cause a stack overflow.

Restarting a function should not actually involve the function calling itself. My approach does indeed work for an actual function because under normal circumstances it would not loop; the loop is only there to provide the restarting point. Here's a more practical example to try out:

This example has many points where the function should restart. Since there is no real code outside the While..WEnd loop, the entire function is essentially being restarted.

definitely a good idea that accomplishes the goal. my brain has just been in recursive mode for a few weeks now, with how much i've been playing with. i can definitely recognize the 384 recursive depth limit as a point of failure to my idea, in addition to the inherent efficiency loss with any recursive solution. It just sounded to me like a situation where the recursion level was not likely to be exceeded, and that was the direction my brain jumped first.
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...