Sign in to follow this  
Followers 0

Function usage question

13 posts in this topic

Posted

I will allow one stab at my ignorance per user :)

Why doesn't $iOne evaluate?

$iApple = 0
$iOne = 0
_test($iApple)
msgbox(0,"",$iOne)


Func _test($iOne)
    $iOne+=10
    Return
EndFunc

Share this post


Link to post
Share on other sites



Posted (edited)

$iOne in your function is a local variable, unrelated to the global variable with the same name. You'd need to pass it ByRef for the function to update the Global variable.

EDIT:Actually the way you're doing it, you'd need to use a different variable name in the function call Func _test($iTwo), because the variable inside your function is actually referencing the $iApple variable that was passed to the function.

Edited by BrewManNH

Share this post


Link to post
Share on other sites

Posted (edited)

Then that makes me ask... why is it required to declare the same amount of arguments as there are passed from the function? What's the point of it?

For example:

$iApple = 0
$iOne = 0
_test($iApple)
msgbox(0,"",$iOne)

Func _test($iOne, $iTwo)
  $iOne+=10  
   Return
EndFunc

$iApple = 0
$iOne = 0
_test($iApple)
msgbox(0,"",$iOne)

Func _test()
  $iApple+=1
   Return
EndFunc

Neither are allowed because of

ERROR: _test() called by a previous line with 1 arg(s)

Edited by mechaflash213

Share this post


Link to post
Share on other sites

Posted

Not sure what you mean, please explain.

Share this post


Link to post
Share on other sites

Posted (edited)

That isn't required as you have the option for optional parameters and is totally unrelated.

Default is that the value is passed down to the Func and you cannot screw up the original content of the Variable used in the Func call.

Edited by Jos

Share this post


Link to post
Share on other sites

Posted

See my example from the previous post.

Share this post


Link to post
Share on other sites

Posted

See my example from the previous post.

Have you RTFMed at all before asking these questions?

It seems to me pretty clear that it is impossible to call a FUNC with a Pararmeter and have the FUNC declared without any.

Share this post


Link to post
Share on other sites

Posted (edited)

Have you RTFMed at all before asking these questions?

There's your one poke :) . I have the help file in front of me about the Func... Return ... EndFunc operation. I'm sorry that I don't get it as I've scripted mainly in windows batch, and functions technically don't exist, you can mimic them however, but it doesn't follow all the same rules as AutoIT has for functions.

I think I got it now tho... tell me if I'm wrong:

$iApple gets passed as an argument and $iOne captures it, no matter what.

If you don't declare Const or ByRef, $iOne doesn't change $iApple, however, you can perform operations against $iApple in the function.

If $iOne is declared a Const in the function, nothing is allowed to change it, however you can still change the value of $iApple.

If $iOne is declared as a ByRef, whatever $iOne evaluates to is mirrored to $iApple inside and outside of the function.

And am I right to say that RTFM evaluates to "Read The Fucking Manual" ? If not sorry for cussing ;)

Edited by mechaflash213

Share this post


Link to post
Share on other sites

Posted

Melba23

Just to be clear, if you just assign a value to a variable before declaring it, it is defaulted to a Global type? Same with DIM ing a variable?

Share this post


Link to post
Share on other sites

Posted

mechaflash213,

It is best not to use Dim as it forces AutoIt to determine the scope of the variable using its own simplistic logic - within a function = Local, elsewhere = Global. So merely declaring a variable does not set its scope to Global unless it is outside a function. It is much better coding practise to declare the scope explicitly yourself - then you can be sure that you have what you want. :)

M23

Share this post


Link to post
Share on other sites

Posted

And am I right to say that RTFM evaluates to "Read The Fucking Manual" ?

That could be one interpretation, although I prefer "Read The Fantastic Manual".

Share this post


Link to post
Share on other sites

Posted

The F in RTFM could be many of which one is Fucking, but I always use one of these variations:Funny/Fancy/Fantastic ..... :)

Share this post


Link to post
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
Sign in to follow this  
Followers 0