← ACARA Digital Technologies Years 9–10

Passing a function as an argument (callback)

A function passed as an argument, without brackets, to another function that calls it later. Examples: nums.forEach(show) or nums.map(n => n * 2).

Understanding Passing a function as an argument (callback) in ACARA Digital Technologies Years 9–10

What it does: isPass has no brackets, so the function itself is passed to filter, which calls it on each item. You pass double as a callback, and map calls it once for every item in the array. The arrow function n => n * 2 is the callback. Adding brackets after 2 breaks the expression because a number is not a function.

Common mistakes: Brackets after show call the function straight away with no argument. forEach needs the function itself, so write nums.forEach(show);. An arrow function uses => and not ->. The callback should be n => n * 2.

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 681–707) and word search (Modules 629–707).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which line correctly passes the function show to forEach as a callback?
nums.forEach(show());nums.forEach(show);
Matches
nums.forEach(show());
↓
The brackets after show call the function straight away, so its result (undefined) is passed instead of the function. Write forEach(show).
Applying
Meanings
Term
callback
Meaning
A function passed as an argument, without brackets, to another function that calls it later. Examples: nums.forEach(show) or nums.map(n => n * 2).
Practice
const nums = [1, 2, 3];
Testing
Gaps
nums.forEach();
Check Answer
Spots
1const nums = [1, 2, 3];
2nums.forEach(show());

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

The 'Hermit Crab' badge — Definition Collector, 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 line correctly passes the function show to forEach as a callback?
A. nums.forEach(show());
B. nums.forEach(show); ✓
Writing show without brackets passes the function itself. show() would call it straight away and pass its result instead.
2. True or false: in nums.map(double), the map method is the one that calls double for each item.
A. False
B. True ✓
You pass double as a callback, and map calls it once for every item in the array.
3. Which is an arrow function written directly in the call as a callback?
A. nums.map(n => n * 2); ✓
B. nums.map(n => n * 2());
The arrow function n => n * 2 is the callback. Adding brackets after 2 breaks the expression because a number is not a function.
4. Why does filter need a callback function?
A. It uses the callback to decide whether to keep each item ✓
B. It uses the callback to sort the array in order
filter runs your callback on each item. If the callback returns true, the item is kept in the new array.
5. What happens with nums.forEach(show()) if show returns undefined?
A. show runs once straight away and undefined is passed, which causes an error ✓
B. show runs once for each item in nums
The brackets call show immediately. forEach then receives undefined, not a function, so it cannot call it for each item.

Match the Pairs

nums.forEach(show); → Passes the function show as a callback, with no brackets, so forEach calls it once for each item
nums.map(n => n * 2); → Writes an arrow function directly in the call as a callback that map runs on every item
To pass a function as a callback, write its name without brackets → scores.filter(isPass);
forEach, map and filter each need a callback so they know what to do with every item → names.forEach(name => console.log(name));
nums.forEach(show()); → The brackets after show call the function straight away, so its result (undefined) is passed instead of the function. Write forEach(show).
nums.map(n * 2); → This passes the value n * 2, not a function. The callback needs an arrow function: map(n => n * 2).

Key Term

callback
A function passed as an argument, without brackets, to another function that calls it later. Examples: nums.forEach(show) or nums.map(n => n * 2).

Fill in the Blanks

1. function show(n) { console.log(n); } nums.forEach(show);
Hint: Type the name of the function being passed as the callback, with no brackets after it.
2. const doubled = nums.map(n => n * 2);
Hint: Type the arrow that turns n into the callback function written inside the call.
3. const big = nums.filter(n => n > 10);
Hint: Type the array method that keeps only the items for which the callback returns true.
4. nums.forEach(n => console.log(n));
Hint: Type the array method that runs the callback on every item without building a new array.
5. function repeat(action) { action(); } repeat(() => console.log("Hi"));
Hint: Type the parameter name that holds the callback, so repeat can call it with brackets.

Spot the Error

Question 1
A const nums = [1, 2, 3];
B nums.forEach(show());
C function show(n) {
D console.log(n);
E }
Line B — Brackets after show call the function straight away with no argument. forEach needs the function itself, so write nums.forEach(show);
Question 2
A const nums = [1, 2, 3];
B let d = nums.map(n -> n * 2);
C console.log(d);
Line B — An arrow function uses => and not ->. The callback should be n => n * 2.
Question 3
A const nums = [4, 12, 20];
B const isBig = n => n > 10;
C let big;
D big = nums.filter(isBig());
E console.log(big);
Line D — isBig() runs the function right now with no value, and filter gets false instead of a function. Pass the function name only: nums.filter(isBig).
Question 4
A [1, 2, 3].forEach(n) => {
B console.log(n * 2);
C });
Line A — The bracket after n closes the forEach call too early. The arrow function must sit inside the brackets: forEach(n => {
Question 5
A const nums = [3, 6, 9];
B const half = n => { n / 2 };
C const h = nums.map(half);
D console.log(h);
Line B — Curly braces need an explicit return. Without one the callback returns undefined, so map builds an array of undefined. Use n => n / 2 or { return n / 2; }.

Student with a class code? Join your class →