← ACARA Digital Technologies Years 7–8

return versus console.log inside a function

Hands a value back to the code that called the function so it can be stored or used. console.log only shows a value in the console, so a function that only logs gives back undefined.

Understanding return versus console.log inside a function in ACARA Digital Technologies Years 7–8

What it does: total only gets the answer 5 if add returns it. Logging alone would leave total as undefined. The function's job is to display a value, not to hand it back, so console.log is the right tool. add only logs the sum. It has no return, so the call gives back undefined. The 5 is shown in the console but is not stored. return ends the function straight away. Any code after it in the function is skipped. return hands 25 back, but nothing stores or displays it, so the student sees no output.

Common mistakes: A function body must open with a curly brace {, not a bracket (. The line should be function add(a, b) {. Then return a + b can hand the sum back to the caller. console.log only shows the value in the console. It does not give it back, so result would be undefined. The line should be return n * n; so the value goes back to the caller.

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 611–623) and word search (Modules 611–634).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A function has the line return total; What does that line do?
Hands total back to the code that called the functionOnly prints total in the console and gives nothing back
Matches
function double(n) { console.log(n * 2); } let result = double(4); console.log(result); // undefined
↓
The function logs the value instead of returning it, so result is undefined. Use return n * 2; instead.
Applying
Meanings
Term
return
Meaning
Hands a value back to the code that called the function so it can be stored or used. console.log only shows a value in the console, so a function that only logs gives back undefined.
Practice
function double(n) {
Testing
Gaps
n * 2;
Check Answer
Spots
1function add(a, b) (
2 return a + b;

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

The 'Penguin' badge — List Waddler, 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. A function has the line return total; What does that line do?
A. Hands total back to the code that called the function ✓
B. Only prints total in the console and gives nothing back
return sends the value back to the caller so it can be stored or used. Printing is the job of console.log.
2. function add(a, b) { console.log(a + b); } let answer = add(2, 3); What is stored in answer?
A. 5
B. undefined ✓
add only logs the sum. It has no return, so the call gives back undefined. The 5 is shown in the console but is not stored.
3. Which function lets you save its result in a variable?
A. function double(n) { return n * 2; } ✓
B. function double(n) { console.log(n * 2); }
Only the function with return gives a value back. The other one just displays it and the variable would get undefined.
4. What does console.log(total) do inside a function?
A. Sends the value back to the line that called the function
B. Shows the value in the console but does not give it back ✓
console.log only displays a value for you to read. Use return to give a value back to the caller.
5. You want to use a function's answer in a later calculation, like let cost = price(3) + 5;. What should price() do?
A. Use return to give back the price ✓
B. Use console.log to show the price
The calculation needs a real value. If price() only logs, it gives back undefined and the sum would not work properly.

Match the Pairs

function add(a, b) { return a + b; } → Hands the sum back to the caller, so it can be stored in a variable or used in other code.
function add(a, b) { console.log(a + b); } → Only shows the sum in the console. The function gives back undefined, so there is nothing to store.
To save a function's answer in a variable, the function must return the value. → let total = add(2, 3); // add uses return
If you only want to display a message for the user to read, console.log is enough. → function showScore(score) { console.log(score); }
function double(n) { console.log(n * 2); } let result = double(4); console.log(result); // undefined → The function logs the value instead of returning it, so result is undefined. Use return n * 2; instead.
function square(n) { return n * n; } square(5); → The returned value is never used. Store it with let answer = square(5); or show it with console.log(square(5)).

Key Term

return
Hands a value back to the code that called the function so it can be stored or used. console.log only shows a value in the console, so a function that only logs gives back undefined.

Fill in the Blanks

1. function double(n) { return n * 2; }
Hint: Type the keyword that hands the result back to the code that called the function.
2. function square(n) { return n * n; } let result = square(4); console.log(result);
Hint: Type the name of the function whose returned value is stored in result.
3. function show(n) { console.log(n * 2); } let x = show(5); console.log(x); // undefined
Hint: Type the value x holds when the function only logs and never hands anything back.
4. function add(a, b) { return a + b; } let sum = add(2, 3); console.log(sum);
Hint: Type the command that displays the stored value in the console.
5. function getAge() { return 13; } let age = getAge() + 1; console.log(age);
Hint: Type the keyword that lets the call be used in a calculation.

Spot the Error

Question 1
A function add(a, b) (
B return a + b;
C }
D let total = add(2, 3);
E console.log(total);
Line A — A function body must open with a curly brace {, not a bracket (. The line should be function add(a, b) {. Then return a + b can hand the sum back to the caller.
Question 2
A function square(n) {
B console.log(n * n);
C }
D let result = square(4);
E console.log(result);
Line B — console.log only shows the value in the console. It does not give it back, so result would be undefined. The line should be return n * n; so the value goes back to the caller.
Question 3
A function getGreeting(name) {
B 'Hi ' + name;
C }
D let msg = getGreeting('Sam');
E console.log(msg);
Line B — The text is joined but never returned, so the function gives back undefined and msg holds undefined. The line should be return 'Hi ' + name;
Question 4
A function area(w, h) {
B let a = w * h;
C console.log(a);
D }
E let size = area(3, 4);
Line C — Logging a only displays it. The function then gives back undefined, so size would be undefined. The line should be return a; so the area is handed back to the caller.
Question 5
A function isEven() {
B return n % 2 === 0;
C }
D console.log(isEven(6));
Line A — The function uses n but has no parameter to receive the value, so n is not defined inside it. The line should be function isEven(n) {

Student with a class code? Join your class →