<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.autoitscript.com/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Billo</id>
	<title>AutoIt Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://www.autoitscript.com/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Billo"/>
	<link rel="alternate" type="text/html" href="https://www.autoitscript.com/wiki/Special:Contributions/Billo"/>
	<updated>2026-04-17T02:42:19Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Arrays&amp;diff=11954</id>
		<title>Arrays</title>
		<link rel="alternate" type="text/html" href="https://www.autoitscript.com/w/index.php?title=Arrays&amp;diff=11954"/>
		<updated>2013-11-03T11:19:26Z</updated>

		<summary type="html">&lt;p&gt;Billo: /* Multi Dimensional Arrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;An &#039;&#039;&#039;array&#039;&#039;&#039; is a [[Data_Structures|data structure]] which stores elements (variables) aligned in a computer&#039;s memory.  Arrays are referenced by an [[identifier]] (variable name) and an index specifying a desired element.  &lt;br /&gt;
&lt;br /&gt;
The array concept seems complex but the concept can be easily grasped.  Why are arrays so important?  The array is a fundamental data structure which is often found in a vast amount of programs.  &lt;br /&gt;
&lt;br /&gt;
This tutorial targets people who are beginners.  To understand how arrays work, it is imperative to try out and modify the provided samples.  Make sure that each concept is understood before the next concept is attempted.  This tutorial assumes the use of the SciTE editor.  There is a minimal version of SciTE included in the latest stable release of AutoIt (version 3.2.0.1 and above).&lt;br /&gt;
&lt;br /&gt;
== Declaring Arrays in AutoIt ==&lt;br /&gt;
&lt;br /&gt;
An array is declared in the same manner as a variable in AutoIt.  The difference is that for an array, extra information on how many elements are to be included in the array must be specified.  This information is provided by adding brackets after the identifier and a number indicating how many elements the array will possess. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;autoit&#039;&amp;gt;&lt;br /&gt;
Global $arr[4] ; Will make space for 4 elements.&lt;br /&gt;
Local  $arr[1] ; Will make space for 1 element.&lt;br /&gt;
Dim    $arr[3] ; Will make space for 3 elements.  Note: Avoid using Dim.  Use Global or Local instead.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In AutoIt, a variable may be converted to an array by either using the {{Help File|ReDim}} keyword or assigning a function which returns an array to the variable.  &lt;br /&gt;
&lt;br /&gt;
For example, the function {{Help File|StringSplit}} returns an array which will be assigned to $arr.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;autoit&#039;&amp;gt;&lt;br /&gt;
Local $arr = StringSplit(&amp;quot;This is my string. I want to split it in sentences.&amp;quot;, &#039;.&#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now to make really certain we have an array from StringSplit, we should check it with the {{Help File|IsArray}} built-in function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;autoit&#039;&amp;gt;&lt;br /&gt;
If IsArray($arr) Then &lt;br /&gt;
     ; Do work on the array&lt;br /&gt;
EndIf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Assigning Data to Array Elements ==&lt;br /&gt;
&lt;br /&gt;
When we declare the array we make some room in memory for future data.  We want to assign some data to the items in the array.  Now here is the catch. The array always starts at index zero.  So, the first element in the array will be accessed by zero, the second element in the array is accessed at by one and so on.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;autoit&#039;&amp;gt;&lt;br /&gt;
 Local $arr[3] ; Make room for three elements&lt;br /&gt;
 ;Assign some data&lt;br /&gt;
 $arr[0]=&amp;quot;Element 1&amp;quot;&lt;br /&gt;
 $arr[1]=&amp;quot;Element 2&amp;quot;&lt;br /&gt;
 $arr[2]=&amp;quot;Element 3&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also assign all the data in one smack like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;autoit&#039;&amp;gt;&lt;br /&gt;
Local $arr[3] = [&amp;quot;element 1&amp;quot;, &amp;quot;element 2&amp;quot;, &amp;quot;element 3&amp;quot;]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This zero-based indexing is quite common in most computer languages, but it can be a source of headaches to beginners until it becomes second nature to them.  For example, every time you want to loop through a range of elements and the range includes the last element, you have to subtract one from the number of items your array is holding, to get the index of the last item. I.E.,  An array with three elements has a last index of two.&lt;br /&gt;
&lt;br /&gt;
So if you don&#039;t take zero-based indexing into consideration in your code, you may ask for something outside the memory area set aside for the array. When you do, you get an error message (&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Array variable has incorrect number of subscripts or subscript dimension range exceeded&amp;lt;/font&amp;gt;) and your script will cease execution.&lt;br /&gt;
&lt;br /&gt;
== Accessing Data in Arrays ==&lt;br /&gt;
&lt;br /&gt;
Let&#039;s walk all elements in the previous sample:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;autoit&#039;&amp;gt;&lt;br /&gt;
Local $arr[3] = [&amp;quot;element 1&amp;quot;, &amp;quot;element 2&amp;quot;, &amp;quot;element 3&amp;quot;] &lt;br /&gt;
&lt;br /&gt;
For $i = 0 to 3 - 1 ; We have an array with three elements but the last index is two.&lt;br /&gt;
    ConsoleWrite($arr[$i] &amp;amp; @LF)&lt;br /&gt;
Next&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Determine Array Size With UBound ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;3 - 1&amp;quot; construct used in the last sample looked strange.  It is not a good idea to hard-code size like that. So lets improve our sample a little. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;autoit&#039;&amp;gt;&lt;br /&gt;
Local $iMax = 3&lt;br /&gt;
&lt;br /&gt;
Local $arr[$iMax] = [&amp;quot;Element 1&amp;quot;, &amp;quot;Element 2&amp;quot;, &amp;quot;Element 3&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
For $i = 0 to $iMax - 1&lt;br /&gt;
    ConsoleWrite($arr[$i] &amp;amp; @LF)&lt;br /&gt;
Next&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now that&#039;s a bit cleaner. It&#039;s also a lot easier to increase or decrease the size of the array. &amp;lt;br&amp;gt;&lt;br /&gt;
But say you don&#039;t know the size of the array upfront because it may come in a variable size when created dynamically.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;autoit&#039;&amp;gt;&lt;br /&gt;
Local $iMax &lt;br /&gt;
&lt;br /&gt;
Local $data=&amp;quot;Element 1|Element 2|Element 3&amp;quot; &lt;br /&gt;
&lt;br /&gt;
; The string in data will be split into an array everywhere | is encountered &lt;br /&gt;
Local $arr = StringSplit($data, &amp;quot;|&amp;quot;) &lt;br /&gt;
&lt;br /&gt;
If IsArray($arr) Then&lt;br /&gt;
     $iMax = UBound($arr); get array size&lt;br /&gt;
&lt;br /&gt;
     ConsoleWrite(&amp;quot;Items in the array: &amp;quot; &amp;amp; $iMax &amp;amp; @LF)&lt;br /&gt;
&lt;br /&gt;
     For $i = 0 to $iMax - 1; subtract 1 from size to prevent an out of bounds error&lt;br /&gt;
         ConsoleWrite($arr[$i] &amp;amp; @LF)&lt;br /&gt;
     Next&lt;br /&gt;
EndIf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you run the above code you will see that $iMax is four and not three as you might have expected. The reason for this is that the developer of the StringSplit() function thought it was a good idea to use the first item (item zero) to keep a count of valid items in the array. This makes sense in many situations as you now have an array containing data with an index starting at one. So our sample code can now be rewritten like this.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;autoit&#039;&amp;gt;&lt;br /&gt;
Local $iMax&lt;br /&gt;
&lt;br /&gt;
Local $data = &amp;quot;Element 1|Element 2|Element 3&amp;quot;&lt;br /&gt;
&lt;br /&gt;
; The string in data will be split into an array everywhere | is encountered&lt;br /&gt;
Local $arr = StringSplit($data, &amp;quot;|&amp;quot;)  &lt;br /&gt;
&lt;br /&gt;
If IsArray($arr) Then &lt;br /&gt;
    For $i = 1 to $arr[0]&lt;br /&gt;
        ConsoleWrite($arr[$i] &amp;amp; @LF)&lt;br /&gt;
    Next&lt;br /&gt;
EndIf&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another good reason for keeping the count in $arr[0]. When you start to use arrays extensively, you will encounter situations where you have to create an array without knowing how many of the elements you will use. Resizing the array is a relatively expensive operation (in CPU cycles) in most languages.&lt;br /&gt;
&lt;br /&gt;
Now consider our example if our initial array has reserved space for ten items but we end up only using three. In this case iterating the array using UBound will force us to check for empty elements. While iterating with $arr[0] needs no other change than maintaining the correct count in $arr[0].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&#039;autoit&#039;&amp;gt;&lt;br /&gt;
Local $iMax=10&lt;br /&gt;
&lt;br /&gt;
;NOTE: We have added the count in the first element&lt;br /&gt;
&lt;br /&gt;
Local $arr[$iMax] = [3, &amp;quot;Element 1&amp;quot;, &amp;quot;Element 2&amp;quot;, &amp;quot;Element 3&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
;NOTE: We use the count in $arr[0] to indicate the last item and we start from index=1&lt;br /&gt;
For $i = 1 to $arr[0]&lt;br /&gt;
    ConsoleWrite($arr[$i] &amp;amp; @LF)&lt;br /&gt;
Next&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Changing Array Sizes With ReDim ==&lt;br /&gt;
&lt;br /&gt;
As arrays are critical to algorithm implementations, and in AutoIt even more so as there is no other means of grouping data, we have to understand how to let it grow and shrink. This is where the keyword ReDim comes into the picture.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s make an example. We want our array to hold data lines but we don&#039;t know how many items we need. We make a guess, in this case five. Now, we use that array to hold data we get from a loop with a random number of iterations. If the array is too small it should automatically be increased. Before we dump the array to output, we should adjust it to the exact size it is supposed to be.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
Local Const $iMax = 5&lt;br /&gt;
&lt;br /&gt;
; NOTE: We have added the count in the first element&lt;br /&gt;
Local $arr[$iMax] = [0] ; Initiate the array and place a counter in the first element.&lt;br /&gt;
&lt;br /&gt;
; Generate a random number between 0 and 20&lt;br /&gt;
Local Const $iRandom = Random(0, 20, 1)&lt;br /&gt;
&lt;br /&gt;
For $i = 1 to $iRandom&lt;br /&gt;
    ; Check that the array is big enough&lt;br /&gt;
    If UBound($arr) = $i Then&lt;br /&gt;
        ; Resize the array when $i is equal to the element count in the array to prevent subscript error&lt;br /&gt;
        ReDim $arr[$arr[0] + $iMax]&lt;br /&gt;
    EndIf&lt;br /&gt;
&lt;br /&gt;
    $arr[$i] = &amp;quot;Item &amp;quot; &amp;amp; $i ; safely add data to new index element&lt;br /&gt;
&lt;br /&gt;
    $arr[0] = $i ; update the index count for future reference&lt;br /&gt;
Next&lt;br /&gt;
&lt;br /&gt;
; Adjust the array size. This time it is probably downward to the size of&lt;br /&gt;
; $arr[0] + 1 (remember the first item is $arr[0])&lt;br /&gt;
ReDim $arr[$arr[0] + 1] &lt;br /&gt;
&lt;br /&gt;
; Now dump the results&lt;br /&gt;
For $i = 1 to $arr[0]&lt;br /&gt;
    ConsoleWrite(&amp;quot;$arr[&amp;quot; &amp;amp; $i &amp;amp; &amp;quot;]:=&amp;quot; &amp;amp; $arr[$i] &amp;amp;  @LF)&lt;br /&gt;
Next&lt;br /&gt;
&lt;br /&gt;
; Visually check that the values are sound&lt;br /&gt;
ConsoleWrite(&amp;quot;Ubound($arr):=&amp;quot; &amp;amp; UBound($arr) &amp;amp; &amp;quot;, $arr[0]:=&amp;quot; &amp;amp; $arr[0] &amp;amp; &amp;quot;, $iRandom:=&amp;quot; &amp;amp; $iRandom &amp;amp; @LF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note how the array now has first been adjusted to a multiple of $iMax and in the last part adjusted down to a size matching the data items.&lt;br /&gt;
&lt;br /&gt;
== Passing Arrays to Functions ==&lt;br /&gt;
&lt;br /&gt;
There is no special syntax required to pass an array as a function argument unlike a low level language such as C.  The following example demonstrates:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;AutoIt&amp;quot;&amp;gt;&lt;br /&gt;
Local Const $myArray[5] = [1, 2, 3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
displayArray($myArray)&lt;br /&gt;
&lt;br /&gt;
Func displayArray(Const $array)&lt;br /&gt;
    Local Const $arrayLength = UBound($array)&lt;br /&gt;
&lt;br /&gt;
    For $i = 0 To $arrayLength - 1&lt;br /&gt;
        MsgBox($MB_OK, &amp;quot;displayArray&amp;quot;, $array[$i])&lt;br /&gt;
    Next&lt;br /&gt;
EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can not declare the variable to hold the array in the function declaration as an array. So, users could pass on a variable. So you have to check that the variable holds an array before you do array specific operations on it.  The following code example will cause an error:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;AutoIt&amp;quot;&amp;gt;&lt;br /&gt;
Local Const $myArray[5] = [1, 2, 3, 4, 5]&lt;br /&gt;
&lt;br /&gt;
displayArray($myArray)&lt;br /&gt;
&lt;br /&gt;
Func displayArray(Const $array[5])&lt;br /&gt;
    Local Const $arrayLength = UBound($array)&lt;br /&gt;
&lt;br /&gt;
    For $i = 0 To $arrayLength - 1&lt;br /&gt;
        MsgBox($MB_OK, &amp;quot;displayArray&amp;quot;, $array[$i])&lt;br /&gt;
    Next&lt;br /&gt;
EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
During the tutorial you have probably noticed that there is a lot of code that is equal in each sample. I&#039;m especially thinking about the code we have used to output the array content. Let&#039;s make life easier and create a debug function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 Func dbgArray(ByRef $arr, $msg=&amp;quot;&amp;quot;)&lt;br /&gt;
     If $msg &amp;lt;&amp;gt; &amp;quot;&amp;quot; Then &lt;br /&gt;
         ConsoleWrite(&amp;quot;*** &amp;quot; &amp;amp; $msg &amp;amp; &amp;quot; ***&amp;quot; &amp;amp; @LF)&lt;br /&gt;
     EndIf&lt;br /&gt;
&lt;br /&gt;
     For $i = 0 to UBound($arr) - 1&lt;br /&gt;
        ConsoleWrite(&amp;quot;$arr[&amp;quot; &amp;amp; $i &amp;amp; &amp;quot;]:=&amp;quot; &amp;amp; $arr[$i] &amp;amp;  @LF)&lt;br /&gt;
     Next&lt;br /&gt;
&lt;br /&gt;
     ConsoleWrite( &amp;quot;Ubound($arr)=:=&amp;quot; &amp;amp; UBound($arr) &amp;amp; &amp;quot;, $arr[0]:=&amp;quot; &amp;amp; $arr[0] &amp;amp; @LF)   &lt;br /&gt;
 EndFunc &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And let&#039;s make a little function to fill our arrays with something. Note how the ArrayFiller makes sure it works on an array.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 Func ArrayFiller(ByRef $arr)&lt;br /&gt;
     If IsArray($arr) Then &lt;br /&gt;
         ReDim $arr[3] ; Notice we might discard content in this operation&lt;br /&gt;
     Else&lt;br /&gt;
         Local $foo[3]&lt;br /&gt;
         $arr = $foo&lt;br /&gt;
     EndIf &lt;br /&gt;
&lt;br /&gt;
     ;Fill the array&lt;br /&gt;
     $arr[0] = 2&lt;br /&gt;
     $arr[1] = &amp;quot;Fill 1&amp;quot;&lt;br /&gt;
     $arr[2] = &amp;quot;Fill 2&amp;quot;&lt;br /&gt;
 EndFunc &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And finally some code using the new functions&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
   Local $arr1[1]&lt;br /&gt;
   ArrayFiller($arr1)&lt;br /&gt;
   dbgArray($arr1)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code is a test on what happens when we pass a regular variable.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
   Local $arr2&lt;br /&gt;
   ArrayFiller($arr2)&lt;br /&gt;
   dbgArray($arr2)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Returning Arrays From Functions ==&lt;br /&gt;
&lt;br /&gt;
As you could observe, in the previous samples, an array will be passed back and forth with the ByRef keyword infront of the variable holding the array in the function declaration.&lt;br /&gt;
&lt;br /&gt;
We could also have used the Return keyword in a function.&lt;br /&gt;
Lets re-work the ArrayFiller function to do this rather than using a variable ByRef.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 Func ArrayFiller2()&lt;br /&gt;
   Local $arr = [3, &amp;quot;Fill 1&amp;quot;, &amp;quot;Fill 2&amp;quot;]&lt;br /&gt;
   Return $arr&lt;br /&gt;
 EndFunc &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And now we can use the function like this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 Local $foo = ArrayFiller2()&lt;br /&gt;
 dbgArray($foo)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparing Arrays ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;You can not compare complete arrays:&#039;&#039;&#039;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
Local $Array1[3] = [1, 2, 3]&lt;br /&gt;
Local $Array2[3] = [1, 2, 3]&lt;br /&gt;
Local $Array3[4] = [1, 2, 3, 4]&lt;br /&gt;
&lt;br /&gt;
If $Array1 == $Array2 Then ConsoleWrite(&amp;quot;1.) $Array1 is equal to $Array2! which might be correct in some sense.&amp;quot; &amp;amp; @LF); while they contain the same data, the comparison does not work.&lt;br /&gt;
&lt;br /&gt;
If $Array1 == $Array3 Then ConsoleWrite(&amp;quot;2.) $Array1 is equal to $Array3! which is incorrect.&amp;quot; &amp;amp; @LF); even though they&#039;re different in size, it&#039;s incorrectly determined that they&#039;re equal.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I have the impression that such comparisons compare the memory address of the arrays instead of the array elements values. And the addresses are always different for different arrays.  You have to instead, compare all elements one after the other. It might be a good idea to first compare array sizes if that can vary for both the compared arrays!&lt;br /&gt;
&lt;br /&gt;
== Multi Dimensional Arrays ==&lt;br /&gt;
&lt;br /&gt;
Now what is a good explanation of a multi-dimensional array?&lt;br /&gt;
It could be a table where you access one item in the table at a time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
Local  $arr[3][3] = [[1, 2, 3], [2, 3, 4], [3, 4, 5]]&lt;br /&gt;
&lt;br /&gt;
For $i = 0 to UBound( $arr, 1) - 1&lt;br /&gt;
    For $j = 0 to UBound($arr, 2) - 1&lt;br /&gt;
        ConsoleWrite(&amp;quot;$arr[&amp;quot; &amp;amp; $i &amp;amp; &amp;quot;][&amp;quot; &amp;amp; $j &amp;amp; &amp;quot;]:=&amp;quot; &amp;amp; $arr[$i][$j] &amp;amp; @LF)&lt;br /&gt;
    Next &lt;br /&gt;
&lt;br /&gt;
    ConsoleWrite(@LF)&lt;br /&gt;
Next &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can add a number of dimensions not to exceed sixty-four as stated in the help file section [http://www.autoitscript.com/autoit3/docs/appendix/LimitsDefaults.htm AutoIt3 limits/Defaults.&lt;br /&gt;
&lt;br /&gt;
Here is a four dimensional sample. You tell me how that initializer is for readability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt; &lt;br /&gt;
; NOTE: The following is supposed to be all on one line&lt;br /&gt;
; but we use the &amp;quot;_&amp;quot; character to split it into multiple lines for readability&lt;br /&gt;
&lt;br /&gt;
Local $arr[3][3][3][3] = [ _&lt;br /&gt;
		[[[1, 2, 3], [2, 3, 4], [3, 4, 5]], [[1, 2, 3], [2, 3, 4], [3, 4, 5]], [[1, 2, 3], [2, 3, 4], [3, 4, 5]]], _&lt;br /&gt;
		[[[1, 2, 3], [2, 3, 4], [3, 4, 5]], [[1, 2, 3], [2, 3, 4], [3, 4, 5]], [[1, 2, 3], [2, 3, 4], [3, 4, 5]]], _&lt;br /&gt;
		[[[1, 2, 3], [2, 3, 4], [3, 4, 5]], [[1, 2, 3], [2, 3, 4], [3, 4, 5]], [[1, 2, 3], [2, 3, 4], [3, 4, 5]]]]&lt;br /&gt;
&lt;br /&gt;
For $i = 0 To UBound($arr, 1) - 1&lt;br /&gt;
	For $j = 0 To UBound($arr, 2) - 1&lt;br /&gt;
		For $k = 0 To UBound($arr, 3) - 1&lt;br /&gt;
			For $l = 0 To UBound($arr, 4) - 1&lt;br /&gt;
				ConsoleWrite(&amp;quot;$arr[&amp;quot; &amp;amp; $i &amp;amp; &amp;quot;][&amp;quot; &amp;amp; $j &amp;amp; &amp;quot;][&amp;quot; &amp;amp; $k &amp;amp; &amp;quot;][&amp;quot; &amp;amp; $l &amp;amp; &amp;quot;]:=&amp;quot; &amp;amp; $arr[$i][$j][$k][$l] &amp;amp; @LF)&lt;br /&gt;
			Next&lt;br /&gt;
		Next&lt;br /&gt;
	Next&lt;br /&gt;
Next&lt;br /&gt;
&lt;br /&gt;
ConsoleWrite(@LF)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Arrays in Arrays ==&lt;br /&gt;
&lt;br /&gt;
You may save an array in an array element (item). The thing is, there is no way to directly access that array stored in the element. You have to go through a variable to get access to the embedded array which may make your code overly complicated and difficult to debug.&lt;br /&gt;
&lt;br /&gt;
Remember that there may be issues if you pass an array containing arrays into a function and the embedded array is changed inside that function. So, to conclude this, try not to embed arrays within arrays unless you absolutely need to and are prepared to do rigorous testing to make sure your code will always work as expected.&lt;br /&gt;
&lt;br /&gt;
That said, here is an example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
Local $arr[3]&lt;br /&gt;
Local $a1[3] = [2, &amp;quot;a1-1&amp;quot;,&amp;quot;a1-2&amp;quot;]&lt;br /&gt;
Local $a2[3] = [2, &amp;quot;a2-1&amp;quot;,&amp;quot;a2-2&amp;quot;]&lt;br /&gt;
$arr[1] = $a1&lt;br /&gt;
$arr[2] = $a2&lt;br /&gt;
$arr[0] = 2&lt;br /&gt;
Local $dumy&lt;br /&gt;
&lt;br /&gt;
For $i = 1 to $arr[0]&lt;br /&gt;
    $dumy = $arr[$i]&lt;br /&gt;
&lt;br /&gt;
    If IsArray($dumy) Then&lt;br /&gt;
        For $j = 1 to $dumy[0]&lt;br /&gt;
            ConsoleWrite(&amp;quot;$i:=&amp;quot; &amp;amp; $i &amp;amp; &amp;quot;, $dumy[&amp;quot; &amp;amp; $j &amp;amp; &amp;quot;]:=&amp;quot; &amp;amp; $dumy[$j] &amp;amp; @LF)&lt;br /&gt;
        Next&lt;br /&gt;
    Else&lt;br /&gt;
        ConsoleWrite(&amp;quot;!&amp;gt;Oops!, What happened? Expected an array!&amp;quot; &amp;amp; @LF)&lt;br /&gt;
    EndIf&lt;br /&gt;
Next&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More Information ==&lt;br /&gt;
&lt;br /&gt;
AutoIt features a large list of [[User Defined Functions|User-Defined Functions] (UDF), among which is a module supplying extra array functions. You can find a reference on those functions in AutoIt&#039;s Help file as the last main chapter named &#039;User Defined Functions Reference&#039;.&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Billo</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Variables_-_using_Global,_Local,_Static_and_ByRef&amp;diff=11953</id>
		<title>Variables - using Global, Local, Static and ByRef</title>
		<link rel="alternate" type="text/html" href="https://www.autoitscript.com/w/index.php?title=Variables_-_using_Global,_Local,_Static_and_ByRef&amp;diff=11953"/>
		<updated>2013-11-03T11:17:27Z</updated>

		<summary type="html">&lt;p&gt;Billo: /* Passing variables as parameters using ByRef */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Tutorials]]&lt;br /&gt;
These keywords confuse many coders - this tutorial should help make their use clear.&lt;br /&gt;
&lt;br /&gt;
In most programming languages you can define the scope of variables - this determines the visibility or accessibility of the variable from different parts of the program. &lt;br /&gt;
&lt;br /&gt;
In AutoIt there are three possible scopes: &#039;&#039;Global, Local&#039;&#039; and &#039;&#039;Static&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
- A &#039;&#039;&#039;Global&#039;&#039;&#039; variable is visible throughout your script - any part of teh script can both read its value and, importantly, change it.&lt;br /&gt;
&lt;br /&gt;
- A &#039;&#039;&#039;Local&#039;&#039;&#039; variable exists only within the function in which it is declared and is destroyed when the function terminates.  It is invisible to any other function unless it is passed as a parameter.&lt;br /&gt;
&lt;br /&gt;
- A &#039;&#039;&#039;Static&#039;&#039;&#039; variable exists only within the function in which it is declared, but is NOT destroyed when the function ends and can be reused when the function is next entered.  You can assign an initial value to this variable when it is created - if it is already in existence then the assignment is ignored.&lt;br /&gt;
&lt;br /&gt;
Why do we need to scope variables?  Well, it is often useful to have a single variable used throughout the script - perhaps the name of an Ini file which if declared as &#039;&#039;Global&#039;&#039; can then be read by any function that requires it. And &#039;&#039;Local&#039;&#039; variables allow us to save resources by only using variables as long as we need - for example if we create a large array which is only needed within a single function.&lt;br /&gt;
&lt;br /&gt;
We scope variables by using the &#039;&#039;Global, Local&#039;&#039; and &#039;&#039;Static&#039;&#039; keywords when the variables are first declared - look at the examples below.  By default, AutoIt scopes any variables declared in the main body of a script (that is not between a &#039;&#039;Func&#039;&#039; and &#039;&#039;EndFunc&#039;&#039; pair) as &#039;&#039;Global&#039;&#039; and any variables declared within function declarations as &#039;&#039;Local&#039;&#039;. But it is a good idea to explicitly declare your variables to make sure that you get what you want.  For example, you can declare a variable as &#039;&#039;Global&#039;&#039; within a function - although you may get a gentle reminder when you run your script within SciTE that this is not recommended.  But there is no point declaring any variables in the main body of a script as &#039;&#039;Local&#039;&#039; - they will be &#039;&#039;Global&#039;&#039; regardless.  If you use the &#039;&#039;AutoItSetOption(&amp;quot;MustDeclareVars&amp;quot;, 1)&#039;&#039; directive you &#039;&#039;must&#039;&#039; declare the scope of your variables or you will get error messages.&lt;br /&gt;
&lt;br /&gt;
In the Help file pages for &#039;&#039;Global, Local&#039;&#039; and &#039;&#039;Static&#039;&#039; you will also see mention of &#039;&#039;Dim&#039;&#039; and &#039;&#039;ReDim&#039;&#039;.  Using &#039;&#039;Dim&#039;&#039; will declare a variable and satisfy the &#039;&#039;AutoItSetOption&#039;&#039; just mentioned, but will let AutoIt scope the variable using the default settings as described above.  It is much better practice to use &#039;&#039;Global/Local/Static&#039;&#039; to explicitly set the scope you require.  Despite its similar name, &#039;&#039;ReDim&#039;&#039; is only used to resize arrays without destroying the content and is outside the scope of this tutorial.&lt;br /&gt;
&lt;br /&gt;
Let us now look at a simple example when we read and change &#039;&#039;Global&#039;&#039; variables in a function:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 Global $Var_1 = &amp;quot;Variable 1&amp;quot;&lt;br /&gt;
 Global $Var_2 = &amp;quot;Variable 2&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 ; Read the variables&lt;br /&gt;
 MsgBox(0x40040, &amp;quot;Reading&amp;quot;, &amp;quot;In The Main Script&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; &amp;quot;Variable 1 = &amp;quot; &amp;amp; $Var_1 &amp;amp; @CRLF &amp;amp; &amp;quot;Variable 2 = &amp;quot; &amp;amp; $Var_2)&lt;br /&gt;
 &lt;br /&gt;
 ; Read the variables in a function&lt;br /&gt;
 _Function()&lt;br /&gt;
 &lt;br /&gt;
 ; And now read the variables again - see that $iVar_2 has changed&lt;br /&gt;
 MsgBox(0x40040, &amp;quot;Reading&amp;quot;, &amp;quot;Back In The Main Script&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; &amp;quot;Variable 1 = &amp;quot; &amp;amp; $Var_1 &amp;amp; @CRLF &amp;amp; &amp;quot;Variable 2 = &amp;quot; &amp;amp; $Var_2)&lt;br /&gt;
 &lt;br /&gt;
 Func _Function()&lt;br /&gt;
 	; Read the variables&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Reading&amp;quot;, &amp;quot;In The Function&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; &amp;quot;Variable 1 = &amp;quot; &amp;amp; $Var_1 &amp;amp; @CRLF &amp;amp; &amp;quot;Variable 2 = &amp;quot; &amp;amp; $Var_2)&lt;br /&gt;
 	; Now let us change one of the variables WITHIN the function&lt;br /&gt;
 	$Var_2 = &amp;quot;Changed Variable 2&amp;quot;&lt;br /&gt;
 EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Here is a simple example showing how &#039;&#039;Local&#039;&#039; variables are only visible &#039;&#039;inside&#039;&#039; their own function:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 Global $Var_1 = &amp;quot;Variable 1&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 ; Read the variable&lt;br /&gt;
 MsgBox(0x40040, &amp;quot;Reading&amp;quot;, &amp;quot;In The Main Script&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; &amp;quot;Variable 1 = &amp;quot; &amp;amp; $Var_1)&lt;br /&gt;
 &lt;br /&gt;
 ; Now run a function&lt;br /&gt;
 _Function()&lt;br /&gt;
 &lt;br /&gt;
 ; And now try to read Variable 3 OUTSIDE the function&lt;br /&gt;
 MsgBox(0x40040, &amp;quot;Reading&amp;quot;, &amp;quot;Back In The Main Script&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; &amp;quot;Variable 1 = &amp;quot; &amp;amp; $Var_1 &amp;amp; @CRLF &amp;amp; &amp;quot;Variable 3 = &amp;quot; &amp;amp; $Var_3)&lt;br /&gt;
 &lt;br /&gt;
 Func _Function()&lt;br /&gt;
 	; Declare a LOCAL variable&lt;br /&gt;
 	Local $Var_3 = &amp;quot;Variable 3&amp;quot;&lt;br /&gt;
 	; Read the variables&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Reading&amp;quot;, &amp;quot;In The Function&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; &amp;quot;Variable 1 = &amp;quot; &amp;amp; $Var_1 &amp;amp; @CRLF &amp;amp; &amp;quot;Variable 3 = &amp;quot; &amp;amp; $Var_3)&lt;br /&gt;
 EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You should have got errors telling you that &#039;&#039;$Var3&#039;&#039; was &amp;quot;&#039;&#039;possibly used before declaration&#039;&#039;&amp;quot; and an &amp;quot;&#039;&#039;undeclared global variable&#039;&#039;&amp;quot;.  This is because &#039;&#039;$Var3&#039;&#039; exists only &#039;&#039;within&#039;&#039; the function - when the function ends it is destroyed.&lt;br /&gt;
&lt;br /&gt;
This means that we can use the same variable name in different functions without causing a problem - very useful for simple variables which are only used temporarily:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 Function_1()&lt;br /&gt;
 &lt;br /&gt;
 Func Function_1()&lt;br /&gt;
 &lt;br /&gt;
 	; Declare the variable in this function&lt;br /&gt;
 	Local $sString = &amp;quot;Variable in Function One&amp;quot;&lt;br /&gt;
 	; Read the value&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Reading&amp;quot;, &amp;quot;In Function One&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; &amp;quot;Variable = &amp;quot; &amp;amp; $sString)&lt;br /&gt;
 &lt;br /&gt;
 	; Now run Function Two&lt;br /&gt;
 	Function_2()&lt;br /&gt;
 &lt;br /&gt;
 	; Now read the variable again - we still get the variable from this function&lt;br /&gt;
 	; The variable in Function 2 has been destroyed now the function has terminated&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Reading&amp;quot;, &amp;quot;In Function One&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; &amp;quot;Variable = &amp;quot; &amp;amp; $sString)&lt;br /&gt;
 &lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func Function_2()&lt;br /&gt;
 &lt;br /&gt;
 	; Declare a variable with the SAME name&lt;br /&gt;
 	Local $sString = &amp;quot;Variable in Function Two&amp;quot;&lt;br /&gt;
 	; Read this variable - see how we get the value in this function, not the value in Function One&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Reading&amp;quot;, &amp;quot;In Function Two&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; &amp;quot;Variable = &amp;quot; &amp;amp; $sString)&lt;br /&gt;
 &lt;br /&gt;
 EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
AutoIt even allows you to use the same name for &#039;&#039;Global&#039;&#039; and &#039;&#039;Local&#039;&#039; variables, although this is &#039;&#039;not&#039;&#039; recommended as it could easily lead to confusion.  If there is a naming conflict of this kind, AutoIt uses the &#039;&#039;Local&#039;&#039; value as you can see here:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 ; Declare the variable as Global&lt;br /&gt;
 Global $sString = &amp;quot;Global Variable in the Main Script&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 ; Read it&lt;br /&gt;
 MsgBox(0x40040, &amp;quot;Reading&amp;quot;, &amp;quot;In the Main Script&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; &amp;quot;Variable = &amp;quot; &amp;amp; $sString)&lt;br /&gt;
 &lt;br /&gt;
 ; Run the functions which declare Local variables with the same name&lt;br /&gt;
 Function_1()&lt;br /&gt;
 &lt;br /&gt;
 ; And now see that the Global variable still exists unchanged&lt;br /&gt;
 MsgBox(0x40040, &amp;quot;Reading&amp;quot;, &amp;quot;In the Main Script&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; &amp;quot;Variable = &amp;quot; &amp;amp; $sString)&lt;br /&gt;
 &lt;br /&gt;
 Func Function_1()&lt;br /&gt;
 &lt;br /&gt;
 	; Declare a variable with the SAME name as Local to this function&lt;br /&gt;
 	Local $sString = &amp;quot;Variable in Function One&amp;quot;&lt;br /&gt;
 	; Read the value - we get the value of the Local variable in this function, not the Global one&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Reading&amp;quot;, &amp;quot;In Function One&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; &amp;quot;Variable = &amp;quot; &amp;amp; $sString)&lt;br /&gt;
 &lt;br /&gt;
 EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A good use for &#039;&#039;Static&#039;&#039; variables is when you need to maintain a record of a value within a function even though the function itself ends.  An example might be that you want to do something the first time a function is called, but not subsequently:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 Func Foo()&lt;br /&gt;
     ; Set the value only on the first entry into the function&lt;br /&gt;
     Static $iFooCount = -1 &lt;br /&gt;
     ; Increase the value each time we enter the function&lt;br /&gt;
     $iFooCount += 1&lt;br /&gt;
     ; Now look at value in the variable&lt;br /&gt;
     If $iFooCount Then&lt;br /&gt;
         ; Not the first pass because value &amp;gt; 0&lt;br /&gt;
     Else&lt;br /&gt;
         ; First pass because value = 0&lt;br /&gt;
     EndIf&lt;br /&gt;
 EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As with &#039;&#039;Local&#039;&#039; variables, you can use the same name for &#039;&#039;Static&#039;&#039; variables in different functions - even though they are not destroyed when the functions end:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 For $i = 1 To 5&lt;br /&gt;
     _Function_1()&lt;br /&gt;
     _Function_2()&lt;br /&gt;
 Next&lt;br /&gt;
 &lt;br /&gt;
 Func _Function_1()&lt;br /&gt;
     ; Declare the variable as Static - remember this only happens once&lt;br /&gt;
     Static $vVar = 0&lt;br /&gt;
     ; Increase the value of the variable each time we enter the function&lt;br /&gt;
     $vVar += 10&lt;br /&gt;
     ; And show that it has retained its value as the function exits&lt;br /&gt;
     ConsoleWrite(&amp;quot;In Function_1: &amp;quot; &amp;amp; $vVar &amp;amp; @CRLF)&lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func _Function_2()&lt;br /&gt;
     ; Use the same variable name&lt;br /&gt;
     Static $vVar = 0&lt;br /&gt;
     $vVar += 100&lt;br /&gt;
     ConsoleWrite(&amp;quot;In Function_2: &amp;quot; &amp;amp; $vVar &amp;amp; @CRLF)&lt;br /&gt;
 EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
As a general rule, try to have as few &#039;&#039;Global&#039;&#039; variables as you can.  Remember that you can share &#039;&#039;Local&#039;&#039; variables between functions - and even change their values - by passing them as parameters as we will see in the &#039;&#039;ByRef&#039;&#039; section below.  And declaring a variable as &#039;&#039;Static&#039;&#039; allows you retain its value even though the function in which it is used has ended.&lt;br /&gt;
&lt;br /&gt;
=Passing variables as parameters using ByRef=&lt;br /&gt;
When you call a function in AutoIt, you can pass parameters to the function by including them in the parentheses following the function name like this - &#039;&#039;Function(Parameter_1, Parameter_2)&#039;&#039;.  Passing variables in this way means that you can use variables that have been declared as &#039;&#039;Local&#039;&#039; within other functions. Note that the parameters are automatically declared as &#039;&#039;Local&#039;&#039; variables within the called function and so do not need to be declared again in the code.&lt;br /&gt;
&lt;br /&gt;
Variables can be passed as parameters to a function in 2 ways:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;By Value&#039;&#039;&#039; - This is the default. The parameter is treated as a &#039;&#039;Local&#039;&#039; variable within the function to which it is passed and any changes made to it are lost when the function ends. So no changes are made to the &#039;&#039;Local&#039;&#039; variable within the original function.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;By Reference&#039;&#039;&#039; - This is used when &#039;&#039;ByRef&#039;&#039; is added &#039;&#039;before&#039;&#039; the variable name in the parameter list of the function declaration. Now any changes to the variable made in the function to which it is passed also affect the &#039;&#039;Local&#039;&#039; variable in the original function.  If you do not understand why this is such a powerful capability, you need to read the &#039;&#039;Global/Local&#039;&#039; section again!&lt;br /&gt;
&lt;br /&gt;
Here is a small example to show how parameters are passed when calling a function:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 Func_A()&lt;br /&gt;
 &lt;br /&gt;
 Func Func_A()&lt;br /&gt;
 &lt;br /&gt;
 	; Declare variables&lt;br /&gt;
 	Local $iVar_A1 = 10&lt;br /&gt;
 	Local $iVar_A2 = 20&lt;br /&gt;
 	; And read them&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Read&amp;quot;, &amp;quot;Initial read in Func A:&amp;quot; &amp;amp; @CRLF &amp;amp; &amp;quot;Var A1 = &amp;quot; &amp;amp; $iVar_A1 &amp;amp; @CRLF &amp;amp; &amp;quot;Var A2 = &amp;quot; &amp;amp; $iVar_A2)&lt;br /&gt;
 &lt;br /&gt;
 	; Now pass these variables to another function - we use the names we have already declared in this function&lt;br /&gt;
 	Func_B($iVar_A1, $iVar_A2)&lt;br /&gt;
 &lt;br /&gt;
 	; We changed the parameters in Func_B - but nothing happened to the variables in this function&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Read&amp;quot;, &amp;quot;Second read in Func A:&amp;quot; &amp;amp; @CRLF &amp;amp; &amp;quot;Var A1 = &amp;quot; &amp;amp; $iVar_A1 &amp;amp; @CRLF &amp;amp; &amp;quot;Var A2 = &amp;quot; &amp;amp; $iVar_A2)&lt;br /&gt;
 &lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func Func_B($iVar_B1, $iVar_B2) ; There is no need to declare the variables as parameters are automatically Local in scope&lt;br /&gt;
 &lt;br /&gt;
 	; Now read these variables - note that they have the same value as the variables in Func_A&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Read&amp;quot;, &amp;quot;Initial read in Func B:&amp;quot; &amp;amp; @CRLF &amp;amp; &amp;quot;Var B1 = &amp;quot; &amp;amp; $iVar_B1 &amp;amp; @CRLF &amp;amp; &amp;quot;Var B2 = &amp;quot; &amp;amp; $iVar_B2)&lt;br /&gt;
 &lt;br /&gt;
 	; Let us change them&lt;br /&gt;
 	$iVar_B1 = 100&lt;br /&gt;
 	$iVar_B2 = 200&lt;br /&gt;
 &lt;br /&gt;
 	; And confirm that they have changed&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Read&amp;quot;, &amp;quot;Second read in Func B:&amp;quot; &amp;amp; @CRLF &amp;amp; &amp;quot;Var B1 = &amp;quot; &amp;amp; $iVar_B1 &amp;amp; @CRLF &amp;amp; &amp;quot;Var B2 = &amp;quot; &amp;amp; $iVar_B2)&lt;br /&gt;
 &lt;br /&gt;
 	; Now return to the other function&lt;br /&gt;
 	&lt;br /&gt;
 EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As you can see, the &#039;&#039;Local&#039;&#039; variables within &#039;&#039;Func_A&#039;&#039; are read perfectly by &#039;&#039;Func_B&#039;&#039; when passed as parameters, even though they were declared as &#039;&#039;Local&#039;&#039; in &#039;&#039;Func_A&#039;&#039;.  However, although &#039;&#039;Func_B&#039;&#039; changes the value of the variables, the values of the original variables within &#039;&#039;Func_A&#039;&#039; are not changed.&lt;br /&gt;
&lt;br /&gt;
If we want to change the value of the variables in &#039;&#039;Func_A&#039;&#039; from within &#039;&#039;Func_B&#039;&#039;, we need to use the &#039;&#039;ByRef&#039;&#039; keyword as explained above and illustrated here:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
 Func_A()&lt;br /&gt;
 &lt;br /&gt;
 Func Func_A()&lt;br /&gt;
 &lt;br /&gt;
 	; Declare variables&lt;br /&gt;
 	Local $iVar_A1 = 10&lt;br /&gt;
 	Local $iVar_A2 = 20&lt;br /&gt;
 &lt;br /&gt;
 	; And read them&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Read&amp;quot;, &amp;quot;Initial read in Func A:&amp;quot; &amp;amp; @CRLF &amp;amp; &amp;quot;Var A1 = &amp;quot; &amp;amp; $iVar_A1 &amp;amp; @CRLF &amp;amp; &amp;quot;Var A2 = &amp;quot; &amp;amp; $iVar_A2)&lt;br /&gt;
 &lt;br /&gt;
 	; Now pass these variables to another function - but this time we will pass $iVar_A2 By Reference&lt;br /&gt;
 	Func_B($iVar_A1, $iVar_A2)&lt;br /&gt;
 &lt;br /&gt;
 	; We changed the parameters in Func_B - this time we see that we have changed $iVar_A2&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Read&amp;quot;, &amp;quot;Second read in Func A:&amp;quot; &amp;amp; @CRLF &amp;amp; &amp;quot;Var A1 = &amp;quot; &amp;amp; $iVar_A1 &amp;amp; @CRLF &amp;amp; &amp;quot;Var A2 = &amp;quot; &amp;amp; $iVar_A2)&lt;br /&gt;
 &lt;br /&gt;
 EndFunc&lt;br /&gt;
 &lt;br /&gt;
 Func Func_B($iVar_B1, ByRef $iVar_B2) ; Note the ByRef keyword in the function declaration for the second parameter&lt;br /&gt;
 &lt;br /&gt;
 	; Now read these variables - they have the same value as the variables in Func_A&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Read&amp;quot;, &amp;quot;Initial read in Func B:&amp;quot; &amp;amp; @CRLF &amp;amp; &amp;quot;Var B1 = &amp;quot; &amp;amp; $iVar_B1 &amp;amp; @CRLF &amp;amp; &amp;quot;Var B2 = &amp;quot; &amp;amp; $iVar_B2)&lt;br /&gt;
 &lt;br /&gt;
 	; Let us change them&lt;br /&gt;
 	$iVar_B1 = 100&lt;br /&gt;
 	$iVar_B2 = 200&lt;br /&gt;
 &lt;br /&gt;
 	; And confirm that they have changed&lt;br /&gt;
 	MsgBox(0x40040, &amp;quot;Read&amp;quot;, &amp;quot;Second read in Func B:&amp;quot; &amp;amp; @CRLF &amp;amp; &amp;quot;Var B1 = &amp;quot; &amp;amp; $iVar_B1 &amp;amp; @CRLF &amp;amp; &amp;quot;Var B2 = &amp;quot; &amp;amp; $iVar_B2)&lt;br /&gt;
 &lt;br /&gt;
 	; Now return to the other function&lt;br /&gt;
 &lt;br /&gt;
 EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;ByRef&#039;&#039; is a very powerful tool - but use it carefully, or you may find that you have changed a variable when you did not want to!&lt;/div&gt;</summary>
		<author><name>Billo</name></author>
	</entry>
</feed>