← ACARA Digital Technologies Years 7–8

Using a returned value

A function call is replaced by the value it returns, so you can store it in a variable, print it or use it in a calculation like square(2) + square(3).

Understanding Using a returned value in ACARA Digital Technologies Years 7–8

What it does: square(4) is replaced by 16, so console.log prints 16 without needing a variable. The call runs and returns 16, but with no variable or console.log the value is thrown away. Each call is replaced by its returned value: square(2) becomes 4 and square(3) becomes 9. Then 4 + 9 = 13. Storing the returned value in a variable keeps it, so you can use big again later in your program. result holds the number 9 that the call returned. The function is not run again when you use result. Both calls belong in one expression: 4 + 25 is stored in total. A semicolon after the first call ends the statement too early.

Common mistakes: Without brackets and an argument, square is not called, so result stores the function itself instead of 16. It should be let result = square(4);. console.log only prints the answer. It does not give a value back, so square(4) returns undefined and result is undefined. It should be return n * n;.

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
function square(n) { return n * n; } let result = square(4); What is stored in result?
The text square(4), because the call is saved as written16, the value that square(4) returns
Matches
square(4); console.log(square);
↓
The function name is printed without brackets and an argument, so it does not call the function or use its returned value. It should be console.log(square(4));
Applying
Meanings
Term
let result = square(4);
Meaning
A function call is replaced by the value it returns, so you can store it in a variable, print it or use it in a calculation like square(2) + square(3).
Practice
function square(n) {
Testing
Gaps
let result = (4);
Check Answer
Spots
3}
4let result = square;

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

The 'Dolphin' badge — Reef Streak, 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. function square(n) { return n * n; } let result = square(4); What is stored in result?
A. The text square(4), because the call is saved as written
B. 16, the value that square(4) returns ✓
The call square(4) is replaced by the value it returns, 16. That value is what gets stored in result.
2. Which line prints the value that square(4) returns?
A. console.log(square(4)); ✓
B. square(4);
Calling square(4) on its own returns 16 but does nothing with it. Putting the call inside console.log() prints the returned value.
3. What does square(2) + square(3) work out to?
A. 13, because it becomes 4 + 9 ✓
B. 25, because it becomes (2 + 3) squared
Each call is replaced by its returned value: square(2) becomes 4 and square(3) becomes 9. Then 4 + 9 = 13.
4. function square(n) { return n * n; } square(5); What happens to the returned value 25?
A. It is ignored, because it was not stored or printed ✓
B. It is printed to the console automatically
JavaScript does not print a returned value by itself. If you do not store it or use it, the value is thrown away.
5. Which line lets you use the answer from square(6) again later?
A. square(6);
B. let big = square(6); ✓
Storing the returned value in a variable keeps it, so you can use big again later in your program.

Match the Pairs

let result = square(4); → Calls square with 4 and stores the returned value, 16, in the variable result
square(2) + square(3) → Both calls become their returned values, 4 and 9, which are added to give 13
A returned value can be printed straight away by putting the call inside console.log() → console.log(square(4));
The returned value can be ignored, but then it is lost and nothing is stored or shown → square(4);
square(4); console.log(square); → The function name is printed without brackets and an argument, so it does not call the function or use its returned value. It should be console.log(square(4));
let result = square; console.log(result + 1); → The call brackets and argument are missing, so result does not store the returned value. It should be let result = square(4);

Key Term

let result = square(4);
A function call is replaced by the value it returns, so you can store it in a variable, print it or use it in a calculation like square(2) + square(3).

Fill in the Blanks

1. function square(n) { return n * n; } let result = square(4);
Hint: Type the name of the function being called to get a value to store.
2. function square(n) { return n * n; } let result = square(4);
Hint: Type the keyword that sends a value back to the place where the function was called.
3. function square(n) { return n * n; } console.log(square(4));
Hint: Type the name of the function whose returned value gets printed.
4. function square(n) { return n * n; } console.log(square(2) + square(3));
Hint: Type the operator that adds the two returned values together.
5. function square(n) { return n * n; } square(3); // value is ignored let result = square(3);
Hint: Type the keyword that declares a variable so the returned value is kept.

Spot the Error

Question 1
A function square(n) {
B return n * n;
C }
D let result = square;
E console.log(result);
Line D — Without brackets and an argument, square is not called, so result stores the function itself instead of 16. It should be let result = square(4);
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 prints the answer. It does not give a value back, so square(4) returns undefined and result is undefined. It should be return n * n;
Question 3
A function square(n) {
B let answer = n * n;
C answer;
D }
E console.log(square(5));
Line C — The function works out answer but never returns it, so square(5) is replaced by undefined. The line should be return answer;
Question 4
A const square = n => n * n;
B let t = square(2) + square();
C console.log(t);
Line B — square() has no argument, so n is undefined and the sum becomes NaN. Each call needs a value, so it should be square(2) + square(3).
Question 5
A function add(a, b) {
B return;
C }
D let sum = add(3, 4);
E console.log(sum);
Line B — A bare return gives back undefined, so there is nothing useful to store in sum. It should be return a + b;

Student with a class code? Join your class →