← ACARA Digital Technologies Years 9–10

A function with no return gives undefined

The value a function gives back when it has no return statement. console.log only shows a value on screen, so a variable storing the result will hold undefined.

Understanding A function with no return gives undefined in ACARA Digital Technologies Years 9–10

What it does: The return keyword sends n * 2 back to wherever the function was called, so it can be stored or used. greet does a job (printing) and does not need to give a value back, so it quietly returns undefined. greet() prints Hi on screen, but it has no return. So result holds undefined, not the text. There is no return, so square(4) is undefined. Adding 1 to undefined gives NaN. Fix it with return n * n;. The expression a + b is calculated but never returned, so the function call gives back undefined.

Common mistakes: Once the arrow function has curly braces, it needs an explicit return. Without one it gives back undefined, so result holds undefined. It should be const sq = n => n * n; or { return n * n; }. console.log only shows the message on screen, it does not give a value back. The function never reaches a return, so text stores undefined. Line 3 should be return msg;.

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
What does this code print? function add(a, b) { a + b; } console.log(add(2, 3));
5undefined
Matches
function square(n) { console.log(n * n); } let answer = square(4);
↓
console.log only prints the value, it does not return it, so answer stores undefined. Use return n * n; instead.
Applying
Meanings
Term
undefined
Meaning
The value a function gives back when it has no return statement. console.log only shows a value on screen, so a variable storing the result will hold undefined.
Practice
function double(n) {
Testing
Gaps
a + b;
Check Answer
Spots
1const sq = n => { n * n };
2const result = sq(4);

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

The 'Angelfish' badge — Reef Perfectionist, 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. What does this code print? function add(a, b) { a + b; } console.log(add(2, 3));
A. undefined ✓
B. 5
The line a + b; works out 5 but never returns it. With no return statement, the function gives back undefined.
2. Which function gives back a value that can be stored in a variable?
A. function double(n) { console.log(n * 2); }
B. function double(n) { return n * 2; } ✓
Only return hands a value back to the caller. console.log just shows it on screen, so the function gives back undefined.
3. After this code runs, what is stored in result? function greet() { console.log('Hi'); } let result = greet();
A. undefined ✓
B. 'Hi'
greet() prints Hi on screen, but it has no return. So result holds undefined, not the text.
4. True or false: console.log inside a function is the same as return.
A. False: console.log only shows a value, return gives it back ✓
B. True: both send the value back to the caller
console.log displays something on screen for people to read. return passes a value back to the code that called the function.
5. A function only needs to print a message on screen. Does it need a return statement?
A. Yes: every function must have a return
B. No: it does a job, so it can skip return ✓
Some functions do a job, like printing. Others give a value. A job-only function can leave out return, and it gives back undefined.

Match the Pairs

function add(a, b) { a + b; } console.log(add(2, 3)); → Prints undefined, because add never uses return so it gives back nothing
let result = showTotal(5, 4); → result holds undefined when showTotal only uses console.log and has no return
A function that gives a value back to the caller must use return → function double(n) { return n * 2; }
A function that only does a job, like showing a message, can leave out return → function greet(name) { console.log('Hi ' + name); }
function square(n) { console.log(n * n); } let answer = square(4); → console.log only prints the value, it does not return it, so answer stores undefined. Use return n * n; instead.
function area(w, h) { let a = w * h; } console.log(area(3, 2)); → The result is stored in a but never returned, so undefined is printed. Add return a; at the end.

Key Term

undefined
The value a function gives back when it has no return statement. console.log only shows a value on screen, so a variable storing the result will hold undefined.

Fill in the Blanks

1. function add(a, b) { return a + b; }
Hint: Type the keyword that sends a value back to the code that called the function.
2. function hi() { console.log("Hi"); } let x = hi(); console.log(x === undefined);
Hint: Type the value that x holds, because hi() never sends anything back.
3. function sq(n) { return n * n; } let r = sq(3); console.log(r);
Hint: Type the keyword that makes the function give a value back so r stores 9 instead of nothing.
4. function greet() { console.log("Hi"); } let msg = greet(); console.log(msg);
Hint: Type the name of the function being called. It only prints a message and gives back no value, so msg is empty.
5. function sq(n) { n * n; } let r = sq(4); console.log(typeof r);
Hint: Type the operator that reports what kind of value r holds.

Spot the Error

Question 1
A const sq = n => { n * n };
B const result = sq(4);
C console.log(result);
Line A — Once the arrow function has curly braces, it needs an explicit return. Without one it gives back undefined, so result holds undefined. It should be const sq = n => n * n; or { return n * n; }.
Question 2
A function greet(name) {
B const msg = 'Hi ' + name;
C console.log(msg);
D }
E const text = greet('Zoe');
Line C — console.log only shows the message on screen, it does not give a value back. The function never reaches a return, so text stores undefined. Line 3 should be return msg;
Question 3
A function add(a, b) {
B const sum = a + b;
C return;
D }
E console.log(add(2, 3));
Line C — A return with no value gives back undefined, so the call prints undefined instead of 5. It should be return sum;
Question 4
A function avg(a, b) {
B const m = (a + b) / 2;
C m;
D }
E console.log(avg(4, 8));
Line C — Writing the variable name on its own line just evaluates it and throws it away. With no return keyword the function gives back undefined. It should be return m;
Question 5
A function cube(n) {
B console.log(n * n * n);
C }
D const c = cube(3);
Line B — console.log shows the answer but does not return it, so cube(3) gives undefined and c stores undefined. Line 2 should be return n * n * n;

Student with a class code? Join your class →