czardas Posted November 16, 2011 Share Posted November 16, 2011 (edited) I have a function which operates on numbers.One digit is not allowed in the input and it's occurence should throw an error. I could return the input number (without changes, since it is an illegal numeric parameter to start off with) or I could return an empty string. I am leaning towards returning the original number unchanged, since there is less of a chance that subsequent numeric functions will throw further errors. Bear in mind that it is down to the person using the function to check for errors. I just wanted to ask people's opinions about this. Would you return the original number?EditFor some reason (perhaps it's me or a perhaps it returns TRUE automatically), the function is returning 1 when I try to avoid returning a value. That must not be allowed, so I have to make a choice of some kind!See for yourselfMsgBox(0, "No Return", _MyRet(7)) Func _MyRet($iInt) ; Something here Return SetError(6) EndFunc Edited November 16, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 16, 2011 Moderators Share Posted November 16, 2011 (edited) czardas,I would argue that trying to create "less of a chance that subsequent numeric functions will throw further errors" is not the way you should be thinking. If the user has no errorchecking in place and misses the erroneous return, allowing other functions to run with an "illegal numeric parameter" will just confuse matters further and make it even harder to determine where the error actually occurred .I would go for a hard fail return, with @error set and a non-numeric return using something like:ConsoleWrite(_Func() & @CRLF & @error & @CRLF & @extended & @CRLF) Func _Func() Return SetError(99, 77, "Wrong") EndFuncBy the way, functions return 0 unless you ask then to do otherwise: ConsoleWrite(_Func() & @CRLF) Func _Func() EndFuncM23Edit: I have just seen your edit above. You get a return of 1 because it is being set by a successful SetError call - that is why there is a specific parameter within SetError to allow you to return another value. Edited November 16, 2011 by Melba23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
czardas Posted November 16, 2011 Author Share Posted November 16, 2011 I'm glad I asked. Thanks for the useful information. Sometimes I think I have my head on back to front. MsgBox(0, "", 0/0) operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
water Posted November 16, 2011 Share Posted November 16, 2011 (edited) In my UDFs the value I return in case of an error depends on the dataype the function returns when no error occurred. 0: when a numeric value or an object is expected "": when a string or array is expected It's the users responsibility to check that no error occurred so he can process the returned data savely. Edited November 16, 2011 by water My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 More sharing options...
bestsmr Posted November 16, 2011 Share Posted November 16, 2011 hello czardas; I understood that you want to return same value of parameter try to use this: MsgBox(0,"","the return value is " & somefunc(5)) Func somefunc($var) ;some code Return SetError(@error,0,$var) ; it returns same passed parameter EndFunc Link to comment Share on other sites More sharing options...
czardas Posted November 16, 2011 Author Share Posted November 16, 2011 (edited) Thanks bestsmr, but that's what I had already. I'm going to change it now after Melba's insightful comments. And Water, I can't return 1 or -1 since these are valid return values without an error. So it will have to be a string. Something better than -1.#IND Edited November 16, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
water Posted November 16, 2011 Share Posted November 16, 2011 (edited) For some reason (perhaps it's me or a perhaps it returns TRUE automatically), the function is returning 1 when I try to avoid returning a value. That must not be allowed, so I have to make a choice of some kind! See for yourself MsgBox(0, "No Return", _MyRet(7)) Func _MyRet($iInt) ; Something here Return SetError(6) EndFuncThat's because every AutoIt function (in this case SetError) returns 1 on success. So the statement Return SetError(6)evaluates to Return 1 Edited November 16, 2011 by water My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 More sharing options...
czardas Posted November 16, 2011 Author Share Posted November 16, 2011 water! Yeah, it kind of surprised me. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
BrewManNH Posted November 16, 2011 Share Posted November 16, 2011 And Water, I can't return 1 or -1 since these are valid return values without an error. So it will have to be a string.1 and -1 aren't valid return values from a function unless you set them to that when you return from that function. If you're creating the function, you determine what the return values are and check for them accordingly. If your function has an error and you decide that returning 1 indicates that there was an error, then that's what the 1 means for that function. There is no default return values from a function other than 0. 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 GudeHow 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 More sharing options...
water Posted November 16, 2011 Share Posted November 16, 2011 Most of the time I return a valid result. If the user doesn't check @error he can't be sure that the return value is correct. You denote that an error occurred in two places: @error and the return value. I think to set @error is enough to tell the user that there was a problem. The user has to check @error. Because if the user doesn't check the return value his script will crash when further processing the returned data. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 More sharing options...
czardas Posted November 16, 2011 Author Share Posted November 16, 2011 (edited) water, that was one of my original concerns. I have a number of functions all obeying the same error checking rules and I have a separate function to do the checking. If the checks fail, then the main function rejects the parameter. Keeping the same parameter would enable the script to continue throwing the same error on the same parameter in different functions, but it won't ever hard crash autoit. Funny kind of logic maybe. Dunno! It's not really a big deal, but I appreciate the responces. Edited November 16, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
czardas Posted November 17, 2011 Author Share Posted November 17, 2011 (edited) I tried returning a string but the same issues occur. MsgBox(0, "Hello World plus zero", _MyRet(7) + 0) Func _MyRet($iInt) ; Something here Return SetError(6, 0, "Hello World") EndFunc Setting the error value equal to infinity seems to work quite well. ;Example1 MsgBox(0, "NaN", _MyRet(7) + 0) ; infinity + 0 ;Example2 Dim $NaN = _MyRet(27) If Not (IsInt($NaN) And IsString($NaN)) Then MsgBox(0, "Infinity", $NaN & " WTF?") Func _MyRet($iInt) ; Something here Return SetError(6, 0, 1/0) EndFunc I doubt that it's a good idea though. Edited November 17, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
czardas Posted November 19, 2011 Author Share Posted November 19, 2011 (edited) Illustration of why it's not a good idea to set infinity as a (hard error) return value. HotKeySet("{ESC}", "_Quit") Dim $infinity = _MyRet() For $i = 0 To $infinity MsgBox(0, "Loop Number " & $i, "Hit escape to quit") Next Func _MyRet() ; Something here Return SetError(1, 0, 1/0) EndFunc Func _Quit() Exit EndFunc i.e. Infinity is still interpreted as a valid number. If IsNumber(1/0) Then MsgBox(0, "", 1/0 & " is a number") The next logical step would be to try using the NUL character. If IsNumber("" + 0) Then MsgBox(0, "Hard Error", "Nope!") If Not (IsNumber(_MyRet()) And IsNumber(_MyRet() + 0)) Then MsgBox(0, "Hard Error", "Yep!") Func _MyRet() ; Something here Return SetError(1, 0, Chr(0)) EndFunc This idea is probably bad, since I imagine it will cause AutoIt to crash under certain unforseen circumstances. Maybe I should decide what to do by tossing a coin. Edited November 19, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
MvGulik Posted November 19, 2011 Share Posted November 19, 2011 (edited) ... The next logical step would be to try using the NUL character.This idea is probably bad, since I imagine it will cause AutoIt to crash under certain unforseen circumstances.Bad: yes.Crash: unlikely, but easy to test ...Issue: not all string related functions act the same way on the Null-character.Just keep it simple.Empty string to indicate a empty none-index array.Zero or something like -1 for number failures if allowed by function. (ergo: if Zero/-1/etc is not part of the valid return range.)Rest ... @error.Only real case where using the function return data as a success or failure indicator is if the function is not returning (by way of Return) any (other) data. (Bool or ByRef type function.)Now go KISS and make up with your Code. Edited November 19, 2011 by MvGulik "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
czardas Posted November 19, 2011 Author Share Posted November 19, 2011 Now go KISS and make up with your Code. The ONLY numeric return values that are not legitimate are the erroneous ones passed to the function in the first place. 0 and 1 and perfectly legitimate results. Anyway, I know what to do now after the opinions expressed. Thanks again. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
jchd Posted November 19, 2011 Share Posted November 19, 2011 Return the "simpliest" non-legitimate value and set @error to non-zero. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
czardas Posted November 19, 2011 Author Share Posted November 19, 2011 Gotya! operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 19, 2011 Moderators Share Posted November 19, 2011 czardas, Sounds very like the advice I gave you back in post #2! M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
czardas Posted November 20, 2011 Author Share Posted November 20, 2011 Yeah Melba! I discover a lot of new things by trying out different alternatives. Sometimes I feel a bit stupid asking dumb questions, but I don't mind too much if others a can benefit from reading this. It's good to get second opinions. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Chimaera Posted November 20, 2011 Share Posted November 20, 2011 I would go for a hard fail return, with @error set and a non-numeric return using something like: ConsoleWrite(_Func() & @CRLF & @error & @CRLF & @extended & @CRLF) Func _Func() Return SetError(99, 77, "Wrong") EndFunc Small Question So your saying we should always set a return in a func? and what do you use the 77 for which i think is the @extended for? That's because every AutoIt function (in this case SetError) returns 1 on success. So the statement Return SetError(6)evaluates to Return 1 Didnt know this at all, so if i made a function or whatever and it gives 1 instead of what i asked it to do ,its my bit of the function, ie the thing asked it to do that is wrong the func is correct because it returns 1? 1 and -1 aren't valid return values from a function unless you set them to that when you return from that function. If you're creating the function, you determine what the return values are and check for them accordingly. If your function has an error and you decide that returning 1 indicates that there was an error, then that's what the 1 means for that function. There is no default return values from a function other than 0. Sorry im confused now so were saying 1 isnt valid now? If Ive just helped you ... miracles do happen. Chimaera CopyRobo() * Hidden Admin Account Enabler * Software Location From Registry * Find Display Resolution * _ChangeServices() 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