Using a returned value

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Using a returned value

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. Which line prints the value that square(4) returns?
A console.log(square(4));
B square(4);
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
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
5. Which line lets you use the answer from square(6) again later?
A square(6);
B let big = square(6);

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1let result = square(4); AThe call brackets and argument are missing, so result does not store the returned value. It should be let result = square(4);
2square(2) + square(3) BThe 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));
3A returned value can be printed straight away by putting the call inside console.log() CCalls square with 4 and stores the returned value, 16, in the variable result
4The returned value can be ignored, but then it is lost and nothing is stored or shown DBoth calls become their returned values, 4 and 9, which are added to give 13
5square(4); console.log(square); Esquare(4);
6let result = square; console.log(result + 1); Fconsole.log(square(4));

Definitions

let result = square(4);

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
function square(n) { return n * n; } let result = ______(4);
2.
function square(n) { ______ n * n; } let result = square(4);
3.
function square(n) { return n * n; } console.log(______(4));
4.
function square(n) { return n * n; } console.log(square(2) _ square(3));
5.
function square(n) { return n * n; } square(3); // value is ignored ___ result = square(3);

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A function square(n) {
B return n * n;
C }
D let result = square;
E console.log(result);
Question 2
A function square(n) {
B console.log(n * n);
C }
D let result = square(4);
E console.log(result);
Question 3
A function square(n) {
B let answer = n * n;
C answer;
D }
E console.log(square(5));
Question 4
A const square = n => n * n;
B let t = square(2) + square();
C console.log(t);
Question 5
A function add(a, b) {
B return;
C }
D let sum = add(3, 4);
E console.log(sum);

Answer Key

Using a returned value
Section A: Quick Questions
Q1
B — 16, the value that square(4) returns
Q2
A — console.log(square(4));
Q3
A — 13, because it becomes 4 + 9
Q4
A — It is ignored, because it was not stored or printed
Q5
B — let big = square(6);
Section B: Match the Pairs
Q1
let result = square(4); → Calls square with 4 and stores the returned value, 16, in the variable result
Q2
square(2) + square(3) → Both calls become their returned values, 4 and 9, which are added to give 13
Q3
A returned value can be printed straight away by putting the call inside console.log() → console.log(square(4));
Q4
The returned value can be ignored, but then it is lost and nothing is stored or shown → square(4);
Q5
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));
Q6
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);
Section C: Definitions
Q1
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).
Section D: Fill in the Blanks
Q1
function square(n) { return n * n; } let result = square(4);
Q2
function square(n) { return n * n; } let result = square(4);
Q3
function square(n) { return n * n; } console.log(square(4));
Q4
function square(n) { return n * n; } console.log(square(2) + square(3));
Q5
function square(n) { return n * n; } square(3); // value is ignored let result = square(3);
Section E: Spot the Error
Q1
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);
Q2
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;
Q3
C — The function works out answer but never returns it, so square(5) is replaced by undefined. The line should be return answer;
Q4
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).
Q5
B — A bare return gives back undefined, so there is nothing useful to store in sum. It should be return a + b;