ravenfyre Posted August 23, 2004 Share Posted August 23, 2004 So it looks like my compass problem is nearing a solution(other post), but a new function I have added is not behaving as expected. I tried to test a similar function seperately to find the same behaviour. Global variables can be altered from anywhere, but Function variables are destroyed the moment the function ends. I use global variables within functions all the time, I just don't know why this one is affected. In this function the global variable is altered within the function. Surely after the function is called the modified values should show up but that's not the case. Even after the function, which modifies these global variables, these variables are still the same as before the function was called. Below is a test script. It changes both test_nums from a value of 0 to 50. The msgbox should return the modified value since it's after the function call, but it instead returns the non-modified value. This leads me to conclude these global variables are acting as function variables and are destroyed the moment the function ends. $test_num1 = 0; $test_num2 = 0; calc($test_num1,$test_num2); msgbox(0, "test", $test_num1&","&$test_num2); Func calc($test_num1,$test_num2); If $test_num1 = 0 Then $test_num1 = 50; EndIf If $test_num2 = 0 Then $test_num2 = 50; EndIf EndFunc Link to comment Share on other sites More sharing options...
this-is-me Posted August 23, 2004 Share Posted August 23, 2004 (edited) Use a different variable name in the function call than in the global variable. Also, use the byref keyword to allow the function to change the global variable. $test_num1 = 0; $test_num2 = 0; calc($test_num1,$test_num2); msgbox(0, "test", $test_num1&","&$test_num2); Func calc(Byref $x,Byref $y); If $x = 0 Then $x = 50; EndIf If $y = 0 Then $y = 50; EndIf EndFunc here, $x only references $test_num1 when passed to the function by calc($test_num1,$test_num2); Edited August 23, 2004 by this-is-me Who else would I be? Link to comment Share on other sites More sharing options...
Westi Posted August 23, 2004 Share Posted August 23, 2004 Or try this: Your var isn't 'global' in the function. $test_num1 = 0; $test_num2 = 0; calc($test_num1,$test_num2); msgbox(0, "test", $test_num1&","&$test_num2); Func calc($test_num1,$test_num2); If $test_num1 = 0 Then global $test_num1 = 50; EndIf If $test_num2 = 0 Then global $test_num2 = 50; EndIf EndFunc Tested... Link to comment Share on other sites More sharing options...
Valik Posted August 24, 2004 Share Posted August 24, 2004 Yet another example of why using "MustDeclareVars" should be on by default... Forcing a keyword to declare the scope would of eliminated this entirely. Link to comment Share on other sites More sharing options...
ravenfyre Posted August 24, 2004 Author Share Posted August 24, 2004 Ah now I get it. You learn something new everyday. Actually that makes 2 things today ^^. Thanks again guys. Link to comment Share on other sites More sharing options...
Nutster Posted August 24, 2004 Share Posted August 24, 2004 Another thing to do is to declare any variable you use in a function as Local. This does not actually apply this time, as the function arguments are automatically local. Take a look at Local in the help file. David NuttallNuttall Computer Consulting An Aquarius born during the Age of Aquarius AutoIt allows me to re-invent the wheel so much faster. I'm off to write a wizard, a wonderful wizard of odd... 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