I always thought the only way data can get into a function is thru through calling the function with parameters. But I stumbled on another way.
I created the script and function below to demonstrate the issue. The function is not meant to do anything important.
The data "three" has made it into my function without being included in the calling parameters.
I always thought of a function like a sandbox. Completely separate of my script. What I do outside the function with variables is safe along as they are not declared global inside the function.
So I was surprised to see data from outside my script get into my function then my function returned that data when I called the function. But I suppose in this case it was not data from a variable that leaked into my function but data from an array.
I fixed the issue in my main script by not using the same name for the array outside and inside the function. I could also clear the array "variables" outside my function when they are no longer needed but in this case I was using my array "variables" and my function at the same relative time.
I searched the forum and found this from 12 years ago. If what is described here is what is happening to my function then this must be from design because it has not been fixed.
https://autohotkey.com/board/topic/39851-loop-makes-local-array-outside-function-in-public-subroutine/
Code: Select all
;
VariableSeperateOfFunction = one two three four
StringSplit,OutputArray,VariableSeperateOfFunction, %A_Space%
Var1 = apple red
var := ReverseVar(Var1)
msgbox,,%a_linenumber%, % var
ReverseVar(VarName)
{
StringSplit,OutputArray,VarName,%a_space%
OutPutArray0++
Loop 3
{
X1 := ( OutPutArray%a_index% a_space X1 )
msgbox %X1%
}
return X1
}