Jump to content

Returning any number of values from a func.


 Share

Recommended Posts

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

Edited by ezzetabi
Link to comment
Share on other sites

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 by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

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

it wasn't actually a nested function, but look at the code

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

_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
endfunc

but 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()
endfunc

run 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 by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

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 by ezzetabi
Link to comment
Share on other sites

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

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