ezzetabi Posted October 27, 2004 Share Posted October 27, 2004 (edited) Some days ago I needed to make a Func that returns more than one value. (and I could not use @error since it was already used) ! After some tries I add a simple but very effective idea. It is enough using byRef! Look in this example, _NextPrevious returns both the number before and the number after the first element passed. Dim $c, $d _NextPrevious (5, $c, $d) MsgBox(0,'','The next of 5 is ' & $d & ' the previous is ' & $c) Exit Func _NextPrevious($num, ByRef $b, ByRef $p) $b = $num - 1 $p = $num + 1 EndFunc This simple but effective idea can be used for having any number of return values! The only difference from the usual way is that you have to use a Variant for result value and you can't nest the func inside other func. Or better, you can, but the second func will just see the 'usual' return value. Maybe it is common knowledge, but, I as I always say, 'noob friendly' advices are never wasted. Edited October 27, 2004 by ezzetabi Link to comment Share on other sites More sharing options...
SlimShady Posted October 27, 2004 Share Posted October 27, 2004 Wow. Cool finding. Really useful. Link to comment Share on other sites More sharing options...
scriptkitty Posted October 27, 2004 Share Posted October 27, 2004 (edited) Ok, well since this is a help forum, here is your same code with the function returning 3 values. $ar=_NP_Array (5) MsgBox(0,'','The next of ' & $ar[0] & ' is ' & $ar[1] & ' the previous is ' & $ar[2]) Exit Func _NP_Array($num) Dim $NP[3] $NP[1] = $num - 1 $NP[2] = $num + 1 $NP[0] = $num Return $np EndFunc More than one way to skin a cat.... of course you only need one input for it. but you can make an array with lots. Edited October 27, 2004 by scriptkitty AutoIt3, the MACGYVER Pocket Knife for computers. Link to comment Share on other sites More sharing options...
ezzetabi Posted October 27, 2004 Author Share Posted October 27, 2004 ... I am feeling quite stupid. Overall since in the past I used that array idea already... But still using byref you can have the advantage of a readable output in nested func (the usual return) where instead returning an array forces using a variant. Link to comment Share on other sites More sharing options...
scriptkitty Posted October 27, 2004 Share Posted October 27, 2004 Oh I agree EZ, byref is very handy, and I use that often as well. This was an FYI post anyhow. AutoIt3, the MACGYVER Pocket Knife for computers. Link to comment Share on other sites More sharing options...
Klaatu Posted October 28, 2004 Share Posted October 28, 2004 1. Aren't all variables variants? 2. The help file clearly states you can't have nested functions, so what are you talking about? My Projects:DebugIt - Debug your AutoIt scripts with DebugIt! Link to comment Share on other sites More sharing options...
scriptkitty Posted October 28, 2004 Share Posted October 28, 2004 (edited) it wasn't actually a nested function, but look at the codeDim $c, $d_NextPrevious (5, $c, $d)MsgBox(0,'','The next of 5 is ' & $d & ' the previous is ' & $c)ExitFunc _NextPrevious($num, ByRef $b, ByRef $p)Â $b = $num - 1Â $p = $num + 1EndFunc_NextPrevious (5, $c, $d) nests $c and $d in it.those are passed to the function, so in one way of thinking, the variables are nexted in the function.The wording is a bit confusing. An actual nested function might be like:func big() $x=1 func small() $x=$x-1 endfunc endfuncbut the functions I really hate that actually will pass the error check are like this:$x=0 big() MsgBox(1,"hi","there"); not that this will ever show. Func small() tooltip($x,0,0) $x=$x+1 big() endfunc func big() $x=$x+1 small() endfuncrun it and you will see the recursion level finally give an error at about 380. Stops a lot of problems.You can run a function in a function safely, and it can save repetitive code but most times I would tend to stay away from it, ex:plus1(5) Func plus1($_x) Global $y $y=$_x add_y() MsgBox(1,"x="&$_x,"Y="&$y) EndFunc Func add_y() $y=$y+1 EndFunc Edited October 28, 2004 by scriptkitty AutoIt3, the MACGYVER Pocket Knife for computers. Link to comment Share on other sites More sharing options...
ezzetabi Posted October 29, 2004 Author Share Posted October 29, 2004 (edited) Also you can eventually make a Func that returns the most important/used value as Return value and less important, maybe useless values in variants... $output = _A-Func($input,$lessimportantoutput) This A-Func whatever it does may return two values if needed or only one if called $a = _A-Func($input,'') that way.. Should, untested. Edited October 29, 2004 by ezzetabi Link to comment Share on other sites More sharing options...
Klaatu Posted October 29, 2004 Share Posted October 29, 2004 But the return value of a function is a variant! You're making it sound like it isn't. My Projects:DebugIt - Debug your AutoIt scripts with DebugIt! Link to comment Share on other sites More sharing options...
scriptkitty Posted October 29, 2004 Share Posted October 29, 2004 pOt`AtO pOtahtO One more tool to use to solve your problem. Now you see how it can be difficult to write such things as a help file. Word something different and you might get more than one meaning. And thereby requiring a paragraph only to explain that. AutoIt3, the MACGYVER Pocket Knife for computers. 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