saywell Posted January 13, 2009 Share Posted January 13, 2009 Hi, I've got a GUI menu (though this isn't a GUI question) that runs several different functions according to which button is clicked. It's all working OK but I now need to develop some error-trapping so something appropriate happens if things go wrong. What I have so far failed to do is work out a way of ending a function prematurely if certain criteria are (or aren't) met. I've tried various ifs and whiles, but if I put an 'endfunc' command anywhere within the function (even if it's in a branch of the conditional pathway) it's interpreted as the end of the function, and Scite tells me off for having an extra endfunc (ie the one in the conditional path, and the one at the proper end of the function. Can anyone point out to me what I'm missing. An example to clarify: I want to run a function that does something only if a variable is less than 10. Otherwise the function should end as soon as the variable value has been checked (and return to main program). Hope that's clear. I'm sure I've just missed something obvious to you guys! Thanks, William Link to comment Share on other sites More sharing options...
KaFu Posted January 13, 2009 Share Posted January 13, 2009 while 1 errorcheck($var) WEnd func errorcheck($var) if $var > 10 then return Else ;Do some error checking EndIf endfunc OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
azure Posted January 13, 2009 Share Posted January 13, 2009 Hi,I've got a GUI menu (though this isn't a GUI question) that runs several different functions according to which button is clicked.It's all working OK but I now need to develop some error-trapping so something appropriate happens if things go wrong.What I have so far failed to do is work out a way of ending a function prematurely if certain criteria are (or aren't) met.I've tried various ifs and whiles, but if I put an 'endfunc' command anywhere within the function (even if it's in a branch of the conditional pathway) it's interpreted as the end of the function, and Scite tells me off for having an extra endfunc (ie the one in the conditional path, and the one at the proper end of the function.Can anyone point out to me what I'm missing.An example to clarify:I want to run a function that does something only if a variable is less than 10. Otherwise the function should end as soon as the variable value has been checked (and return to main program).Hope that's clear. I'm sure I've just missed something obvious to you guys!Thanks,WilliamReturn a value?Return(0) or Return(1) or whatever.Or Just Return Link to comment Share on other sites More sharing options...
saywell Posted January 13, 2009 Author Share Posted January 13, 2009 Return a value?Return(0) or Return(1) or whatever.Or Just ReturnJust skip the rest of the function and return to the main prog to await another GUI button press.EG I want to burn some files to a CD.I check the directory size.If it will fit on one CD it carries on with the rest of the function to burn the CDIf it won't fit on one CD it pops up a msgBox then I want it to skip the burning bits and abort the function, and retun control to the GUI.William Link to comment Share on other sites More sharing options...
azure Posted January 13, 2009 Share Posted January 13, 2009 Just skip the rest of the function and return to the main prog to await another GUI button press. EG I want to burn some files to a CD. I check the directory size. If it will fit on one CD it carries on with the rest of the function to burn the CD If it won't fit on one CD it pops up a msgBox then I want it to skip the burning bits and abort the function, and retun control to the GUI. William Yeah.. use Return to exit a function. You don't have to return a value. Link to comment Share on other sites More sharing options...
FireFox Posted January 13, 2009 Share Posted January 13, 2009 Return isnt generaly used for return something not for exit a function ... Examples : HotKeySet("{F8}", "_F8HK") Global $F8 = False Msgbox(64, 'test', _Func("0")) MsgBox(64, 'test', _Func("1")) MsgBox(64, 'test', _Func("2")) While 1 Sleep(250) If _F8() Then ;if returned right then MsgBox(64, 'test', 'function f8 called !') EndIf WEnd Func _F8HK() Global $F8 = True EndFunc Func _F8() If $F8 = True Then Global $F8 = False Return 1 ;when return 1, the function called return right EndIf Return 0 ;return 0 = wrong EndFunc Func _Func($nb) If $nb = 1 then Return 'right !' ElseIf $nb = 0 then Return 'Wrong !' EndIf Return 'you loose !' EndFunc Cheers, FireFox. Link to comment Share on other sites More sharing options...
saywell Posted January 14, 2009 Author Share Posted January 14, 2009 Return isnt generaly used for return something not for exit a function ... Examples : HotKeySet("{F8}", "_F8HK") Global $F8 = False Msgbox(64, 'test', _Func("0")) MsgBox(64, 'test', _Func("1")) MsgBox(64, 'test', _Func("2")) While 1 Sleep(250) If _F8() Then ;if returned right then MsgBox(64, 'test', 'function f8 called !') EndIf WEnd Func _F8HK() Global $F8 = True EndFunc Func _F8() If $F8 = True Then Global $F8 = False Return 1 ;when return 1, the function called return right EndIf Return 0 ;return 0 = wrong EndFunc Func _Func($nb) If $nb = 1 then Return 'right !' ElseIf $nb = 0 then Return 'Wrong !' EndIf Return 'you loose !' EndFunc Cheers, FireFox. Thanks, Firefox. Sorry for the confusion between 'return' as in 'return a value' (tech-speak) and 'return' as in 'go back to' (everyday-english speak)! So do I understand you correctly in that instead of doing the test within the function you do the test first, then call a different function depending on the result? If so, I presume it's OK to call a function from within another function (sorry if this is a bleeding obvious newbie question!)? If not, I could run the 'while' bit where folder size is tested within the GUI while loop, perhaps? William Link to comment Share on other sites More sharing options...
saywell Posted January 16, 2009 Author Share Posted January 16, 2009 I think the answer is in a recent post.Return seems to do what I want - I'll give it a try later.William Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now