ravenfyre 0 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 Share this post Link to post Share on other sites
this-is-me 6 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? Share this post Link to post Share on other sites
Westi 0 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... Share this post Link to post Share on other sites
Valik 478 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. Share this post Link to post Share on other sites
ravenfyre 0 Posted August 24, 2004 Ah now I get it. You learn something new everyday. Actually that makes 2 things today ^^. Thanks again guys. Share this post Link to post Share on other sites
Nutster 3 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 ConsultingAn Aquarius born during the Age of AquariusAutoIt allows me to re-invent the wheel so much faster.I'm off to write a wizard, a wonderful wizard of odd... Share this post Link to post Share on other sites