Section 1: Introduction
Arrays are automatically passed to functions by reference, i.e., as a pointer to the original array.
That means that anything that you do to the array inside the function affects the original array; this can cause hard-to-trace side effects if you are not careful.
Remember that any changes made to an array in a function will change the original array.
See the "trick" explained below.
Individual array elements are passed just like primitive variables.
Section 2: Passing arrays to functions
To pass an entire array to a function, specify the array name as the argument.
For example, if array hourlyTemperatures is declared as...
var hourlyTemperatures = [];
hourlyTemperatures.length = 24; // sets the array length to 24, or indexes
0 to 23
...the call...
modifyArray(hourlyTemperatures)
...passes array hourlyTemperatures to function modifyArray. [See modifyArray below.]
Section 3: Receiving arrays in functions
There is no special designation for a parameter in the function heading to indicate that the function can receive an entire array through a call.
For example, the function header for modifyArray might be written as...
function modifyArray(receivedArray)
{
receivedArray[0] = 'changed!';
}
Remember that if the function changes any array elements then that change is reflected in the calling program as well.
- Therefore, if the call modifyArray(hourlyTemperatures) is used, then the first element of hourlyTemperatures has the value 'changed!' after the function call.
Section 4: Trick - Passing arrays by value
Because passing by reference can lead to unexpected side effects, there is a trick that can be used to pass an array by value. In order to do so the Array.slice() method must be used.
var hourlyTemperatures = [42, 40, 39, 34, 32, 30, 31, 33, 35, 39, 42, 44,
47, 50, 52, 55, 56, 57, 56, 54, 50, 49, 46, 44, 41];
modifyArray(hourlyTemperatures.slice());
In this case, because the array called the slice function, a copy of the array is sent to the function and any changes to the array made in the function are NOT reflected in the calling program.
- After the function call the hourlyTemperatures array is unchanged.
Section 5: Passing array elements to functions
To pass an element of an array to a function, use the array element (array name and index) as an argument in the call.
For example, the call...
passOneElement(hourlyTemperatures[5]);
...would pass a copy of the array element corresponding to index 5 to passOneElement. [See passOneElement below.]
Section 6: Receiving array elements in functions
For a function to receive an array element from a call, the function heading parameter list simply declares a variable.
For example, the function header for passOneElement might be written as...
function passOneElement (arrayElement)
If the function is called with the line...
passOneElement(hourlyTemperatures[5]);
...then the parameter arrayElement in the function would get a copy of the value stored in hourlyTemperatures [5].
Section 7: Example of arrays as parameters
Section 8: Returning Arrays from Functions
JavaScript can easily return an array from a function by simply including the line
return arrayName;
The following examples, from this reference, demonstrates this.
function createArray()
{
var newArray = [];
newArray[0] = "A";
newArray[1] = "B";
newArray[2] = "C";
newArray[3] = "D";
return newArray;
}
function showArray(theArray)
{
var output = "";
for (var i = 0; i < theArray.length; i++)
{
output += theArray[i] + " ";
}
return output;
}
var myArray = createArray();
alert(showArray(myArray));