Jump to content

Exiting a Function?


S1M1S
 Share

Recommended Posts

I've encountered a snag where I call another function, but I can't get the original one to exit.

For example:

Func1()

Func Func1()
    Func2()
    MsgBox ( 0, "", "Func1" )
EndFunc

Func Func2()
    Func3()
    MsgBox ( 0, "", "Func2" )
EndFunc

Func Func3()
    Func4()
    MsgBox ( 0, "", "Func3" )
EndFunc

Func Func4()
    Func5()
    MsgBox ( 0, "", "Func4" )
EndFunc

Func Func5()
    MsgBox ( 0, "", "Func5" )
EndFunc

But what if I want to call Func2 AND close Func1?

Is there some sort of function which does this:

Func2()
    MsgBox ( 0, "", "Func1" )
EndFunc

Func Func2()
    _Function_Close ( Func1 )
    Func3()
    MsgBox ( 0, "", "Func2" )
EndFunc

Any help would be greatly appreciated ;)

Edited by S1M1S
Link to comment
Share on other sites

  • Moderators

S1M1S,

Look at the Interrupting a running function tutorial in the Wiki - lots of information on how to it there. But it is a bit more complicated then a single line! ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@Melba23

Once again I'm impressed by the time and effort you've put into the wiki ;)

No one should be allowed to post a question on the forum before having read the wiki as a whole :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

The code below shows one way of doing it programmatically within your functions:

Func1()
Func Func1()
    $Return = Func2()
    If $Return > 0 Then Return
    MsgBox(0, "", "Func1")
EndFunc   ;==>Func1
Func Func2()
    $Return = Func3()
    If $Return > 0 Then Return
    MsgBox(0, "", "Func2")
EndFunc   ;==>Func2
Func Func3()
    $Return = Func4()
    If $Return > 0 Then Return
    MsgBox(0, "", "Func3")
    Return 1 ; <<<<<<<<<<<<<<<<<<<<<<<  MsgBox in Func2 will not be run
EndFunc   ;==>Func3
Func Func4()
    $Return = Func5()
    If $Return > 0 Then Return
    MsgBox(0, "", "Func4")
EndFunc   ;==>Func4
Func Func5()
    MsgBox(0, "", "Func5")
    Return 1 ; <<<<<<<<<<<<<<<<<<<<<<<  MsgBox in Func4 will not be run
EndFunc   ;==>Func5

You can use this method so that when you return from the previous function you will run whatever comes after the function call depending upon what happens in the called function. An If...Then statement for instance.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

BrewManNH,

I feel your solution is the one the OP was looking for - I had not taken in that he was calling the new functions from within the running one.

Note to self: Look at the code more closely! ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hey, I took your advice Melba, don't worry :) - Though you do realize I had already been to that page... ;)

And thanks a ton BrewManNH - There's only one problem... I created this thread because I wanted to find out how it worked. Your example is great, and I will be able to mimic it whenever I need to - but how the heck does it work!? What does 'Return'ing 1 do? I know what return does, but returning 1!?

Edited by S1M1S
Link to comment
Share on other sites

It doesn't matter what you value you return. What you will do with the return value does. Simply talking you use the return value to evaluate the successful execution of a function or even use the return value for another function execution or just display the value ;)

-EDIT-

you could also use the Return command to exit a function and simply ignore the return value if you don't need it

Edited by Oneqa
Link to comment
Share on other sites

I think I understand you... but why does the MsgBox not run?

When you jump to Func3 from Func2, the function call is assigned to a variable, in this case $Return. When Func3 ends, it sends the value of 1 back to the calling routine, and the $Return value is now equal to 1 because that is what I told Func3 to "Return" at the end of its execution. I could have set it to "Return" anything, in this case I set it to return the number 1.

The line following the function call is an If...Then statement, that checks the variable $Return, and if its any value that is greater than 0, then it will Return to ITS calling function, rather than continuing the execution of the func2 function. That's why the MsgBox in Func2 won't run because it Returns to Func1 before its ever executed.

I hope that doesn't confuse you too much, I'm horrible at explaining things.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

note that in func5 it return 1 which will result in skipping msgbox for the function that execute it which is func4.

Func Func4()
    $Return = Func5()
    If $Return > 0 Then Return  ;<=== This is true because func5 is returning 1, will immediately exit Func4

Func4 is returning empty string which is not greater than zero so msgbox will appear:

Func Func3()
    $Return = Func4()
    If $Return > 0 Then Return ;<=== This is not true, will skip to next statement
    MsgBox(0, "", "Func3")  ;<=== This is true because func5 is returning 1

As i said before when you issue return you will immediately exit the function and return the value after the "Return" statement.

Link to comment
Share on other sites

[Awesomeness]

I hope that doesn't confuse you too much, I'm horrible at explaining things.

It doesn't matter what you value you return. What you will do with the return value does. Simply talking you use the return value to evaluate the successful execution of a function or even use the return value for another function execution or just display the value

-EDIT-

you could also use the Return command to exit a function and simply ignore the return value if you don't need it

Is there a 'like' button around here somewhere?

[/Awesomeness]

Edited by S1M1S
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...