Jump to content

Exit Function


BALA
 Share

Recommended Posts

I have a function that displays a message box and asks the user if they want to continue. If the user presses Yes, it will go to another function. I want to have it so if the user doesn't press yes, it will exit out of the function. Is what I have correct?

Func Begin()
    $confirm = MsgBox(4, "Confirmation", "Are you sure you want to scan for and delete found temporary files?")
    If $confirm = 6 Then
        BeginREAL()
    Else
    ; Maybe something here? 
    EndIf
EndFunc
Edited by BALA
[font="Comic Sans MS"]BA-LA[/font]http://ba-la.110mb.comJoin my community, CLICK HEREAlternative links to my site:http://www.ba-la.tkhttp://www.ba-la.co.nrContact me if you would like to help with some of my projects: joeythepirate@gmail.com
Link to comment
Share on other sites

Well, I don't want it to exit the script, just the function. Is leaving it blank right, or is that incorrect syntax?

[font="Comic Sans MS"]BA-LA[/font]http://ba-la.110mb.comJoin my community, CLICK HEREAlternative links to my site:http://www.ba-la.tkhttp://www.ba-la.co.nrContact me if you would like to help with some of my projects: joeythepirate@gmail.com
Link to comment
Share on other sites

Well, all I want it to do is end that current function. When that current function ends, it'll go back to the While-loop. So what I want to know is if I leave that part blank, will it just end the function?

I know it works if I leave it blank, I'm just not sure that's not correct syntax.

Edited by BALA
[font="Comic Sans MS"]BA-LA[/font]http://ba-la.110mb.comJoin my community, CLICK HEREAlternative links to my site:http://www.ba-la.tkhttp://www.ba-la.co.nrContact me if you would like to help with some of my projects: joeythepirate@gmail.com
Link to comment
Share on other sites

The Else-part is optional so if you're not planning on doing anything there there's no need to even

add that part. To end the current function use Return. Here's how you could've done it though...

Func Begin()
    $confirm = MsgBox(4, "Confirmation", "Are you sure you want to scan for and delete found temporary files?")
    If $confirm = 6 Then BeginREAL()
EndFunc
Link to comment
Share on other sites

  • 3 years later...

There is actually a different way to do this, that I often find useful. One way was already discussed above- placing the body of the script that you want to return to into a function, and calling it. However, if you have a script that has a main body (i.e. at the top) that calls certain functions already written, this can involve a bit of reorganizing and adjustment. The method described above is best practice, and is what you should use starting out with a new script. If you already have your script written, however, there are two other options I know of which require much less reworking, and operate just as well.

The second method will have the same result, but is not necessarily best practice. This method requires no manipulation of the main body of your script, and instead requires minor changes to the function itself. You can place the body of your function into a Do..until loop, with an until condition as true (i.e. it will never loop, because the until parameter is always true). Once you have the body of your function in a do..until, you can exit the function with ExitLoop. Let's just suppose you had a GUI with a few buttons, each one runs a different function. I won't actually write out the gui code (to avoid unnecessary clutter), so we start with the while loop. Say one buttons runs the "Begin" function. Here's an example of the do..until method:

;; GUI CODE HERE
While 1
   sleep(1000) ; idle around, waiting for user to click a button. 
Wend

Func Begin()
  do
    $confirm = MsgBox(4, "Confirmation", "Are you sure you want to scan for and delete found temporary files?")
      If $confirm <> 6 Then ExitLoop  ; exitloop makes the execution jump to outside the do..until, which basically just ends the function. 
    
    ; Put your code from "BeginREAL()" here, i.e. code to scan and delete temp files. 
  until 1

; execution basically goes here if ExitLoop condition occurs, which then immediately exits the function and returns to the stuff above. 
EndFunc

You also don't need to have a separate function, just for confirmation- just have your IF..THEN statement do either "IF user presses NO then exitloop, ELSE continue", or "IF user DOESN'T press YES then exitloop, ELSE continue."

PS: I find it's a good idea to name function with a preceding underscore, just because then I don't have to worry about the name of my function being an actual autoit command... plus it increases readability, i.e. Begin() vs. _Begin().

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...