The docs wrote:Within a function, any dynamic variable reference such as Array%i% always resolves to a local variable unless no variable of that name exists, in which case a global is used if it exists. If neither exists and the usage requires the variable to be created, it is created as a local variable unless the assume-global mode is in effect. Consequently, a function can create a global array manually (by means such as Array%i% := A_Index) only if it has been defined as an assume-global function.
Common source of confusion: Any non-dynamic reference to a variable creates that variable the moment the script launches. For example, when used outside a function, MsgBox %Array1% creates Array1 as a global the moment the script launches. Conversely, when used inside a function MsgBox %Array1% creates Array1 as one of the function's locals the moment the script launches (unless assume-global is in effect), even if Array and Array0 are declared global.
That's why I wish an assume/force-local mode to be implemented preventing dynamic variable references in functions from resolving to global vars. The wordv2-changes - Variables wrote:Double-derefs and pseudo-arrays created by commands (if any remain?) are now consistent with variables resolved at load-time. That is, a dynamic variable reference will resolve to a (non-super) global only if the function is assume-global or the variable is declared as global in the function. (Important: this also applies to the vVariable option of Gui.)
Functions default to either assume-local or must-declare depending on the setting at the point the function is defined, but this can be overridden by using Global,
Localor Static without a variable name.
Localwithout a variable name in the first line of the function could be used without breaking existing scripts, because it raises an error in the current v1 implementation.
Related: local Vars in Funktionen