← ACARA Digital Technologies Years 9–10

Passing an array to a function

A function with a parameter that receives an array when called, e.g. total([4, 7, 9]). The function can loop over it, return a new array, or change the original array.

Understanding Passing an array to a function in ACARA Digital Technologies Years 9–10

What it does: The array in square brackets is the argument and it is received by the function's parameter. map builds a new array, and returning it gives the caller the new array while nums stays the same. scores is the parameter name. It receives the whole array. Use scores.length if you need the number of items. Inside the function, list is the array [5, 6, 7]. list[0] reads the first item, which is 5. The function receives the same array, not a copy. Mutating methods like push change the original array. The loop adds up the values correctly, but the result is lost unless the function returns sum.

Common mistakes: The variable is called sum, not sums, so this causes a ReferenceError. The function should return sum, the total built up while looping over the array. The closing bracket after the parameter name is missing. It should be function average(nums) {.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 710–727) and word search (Modules 710–742).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which call gives the function total ONE array of scores?
total(4, 7, 9)total([4, 7, 9])
Matches
function total(scores) { let sum = 0; for (const s of scores) sum += s; }
↓
There is no return statement, so the function gives back undefined instead of the total.
Applying
Meanings
Term
function total(scores)
Meaning
A function with a parameter that receives an array when called, e.g. total([4, 7, 9]). The function can loop over it, return a new array, or change the original array.
Practice
function total(scores) {
Testing
Gaps
return list.;
Check Answer
Spots
3 nums.forEach(n => sum += n);
4 return sums;

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Space Rabbit' badge — Launch Pad, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which call gives the function total ONE array of scores?
A. total([4, 7, 9]) ✓
B. total(4, 7, 9)
The square brackets make one array argument. total(4, 7, 9) passes three separate numbers, so scores would only receive 4.
2. You have let myScores = [4, 7, 9]; Which call passes this array to total?
A. total(myScores[])
B. total(myScores) ✓
Just write the variable name as the argument. The empty brackets [] are not valid when passing an array.
3. In function total(scores) { ... }, what is scores?
A. A number that stores how many items are in the array
B. A parameter that holds the array passed in when the function is called ✓
scores is the parameter name. It receives the whole array. Use scores.length if you need the number of items.
4. Which loop header reads every item of the array scores inside the function?
A. for (let i = 0; i < scores.length; i++) ✓
B. for (let i = 0; i < scores; i++)
scores is an array, not a number. Compare i with scores.length so the loop runs once for each item.
5. What does this print? function first(list) { return list[0]; } console.log(first([5, 6, 7]));
A. 5 ✓
B. [5, 6, 7]
Inside the function, list is the array [5, 6, 7]. list[0] reads the first item, which is 5.

Match the Pairs

function total(scores) { let sum = 0; for (const s of scores) sum += s; return sum; } → The function receives an array in its parameter and loops over it to add up every item.
function addBonus(scores) { scores.push(10); } → The function changes the original array that was passed in by adding an item to it.
Call a function and give it an array as the argument → total([4, 7, 9]);
A function returns a new array and leaves the original array unchanged → function doubleAll(nums) { return nums.map(n => n * 2); }
function total(scores) { let sum = 0; for (const s of scores) sum += s; } → There is no return statement, so the function gives back undefined instead of the total.
function largest(scores) { return Math.max(scores[]); } → Empty square brackets after the parameter name are invalid syntax. The array should be used by name or an index.

Key Term

function total(scores)
A function with a parameter that receives an array when called, e.g. total([4, 7, 9]). The function can loop over it, return a new array, or change the original array.

Fill in the Blanks

1. function count(list) { return list.length; }
Hint: Type the array property that tells you how many items the parameter holds.
2. function double(nums) { return nums.map(n => n * 2); }
Hint: Type the array method that builds a new array by changing every item, so the original array stays the same.
3. function addItem(list, item) { list.push(item); }
Hint: Type the array method that adds an item to the end of the original array.
4. const scores = [4, 7, 9]; addItem(scores, 5); console.log(scores);
Hint: Type the name of the array variable you are passing to the function as the first argument.
5. function show(names) { for (const n of names) console.log(n); }
Hint: Type the keyword that loops over each value inside the array parameter.

Spot the Error

Question 1
A function total(nums) {
B let sum = 0;
C nums.forEach(n => sum += n);
D return sums;
E }
Line D — The variable is called sum, not sums, so this causes a ReferenceError. The function should return sum, the total built up while looping over the array.
Question 2
A function average(nums {
B let sum = 0;
C nums.forEach(n => sum += n);
D return sum / nums.length;
E }
Line A — The closing bracket after the parameter name is missing. It should be function average(nums) {.
Question 3
A function doubleAll(nums) {
B nums.map(n => n * 2);
C }
D const a = [1, 2, 3];
E console.log(doubleAll(a));
Line B — map() builds a new array, but without return the function throws it away and gives back undefined. It should be return nums.map(n => n * 2);.
Question 4
A function bonusAll(marks) {
B let n = marks.length;
C for (let i = 0; i < n; i++) {
D let x = marks[i] + 5;
E }
F }
Line D — This stores the new mark in a local variable, so the original array is never changed. To change the array passed in, it should be marks[i] += 5;.
Question 5
A const marks = [70, 85, 60];
B function largest(nums) {
C return Math.max(nums);
D }
E console.log(largest(marks));
Line C — Math.max needs separate numbers, so passing the whole array gives NaN. The array must be spread with ...nums: Math.max(...nums).

Student with a class code? Join your class →