A function with no return gives undefined

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

A function with no return gives undefined

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does this code print? function add(a, b) { a + b; } console.log(add(2, 3));
A undefined
B 5
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; }
3. After this code runs, what is stored in result? function greet() { console.log('Hi'); } let result = greet();
A undefined
B 'Hi'
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
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

Match the Pairs

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

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

Definitions

undefined

Fill in the Blanks

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

1.
function add(a, b) { ______ a + b; }
2.
function hi() { console.log("Hi"); } let x = hi(); console.log(x === _________);
3.
function sq(n) { ______ n * n; } let r = sq(3); console.log(r);
4.
function greet() { console.log("Hi"); } let msg = _____(); console.log(msg);
5.
function sq(n) { n * n; } let r = sq(4); console.log(______ r);

Spot the Error

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

Question 1
A const sq = n => { n * n };
B const result = sq(4);
C console.log(result);
Question 2
A function greet(name) {
B const msg = 'Hi ' + name;
C console.log(msg);
D }
E const text = greet('Zoe');
Question 3
A function add(a, b) {
B const sum = a + b;
C return;
D }
E console.log(add(2, 3));
Question 4
A function avg(a, b) {
B const m = (a + b) / 2;
C m;
D }
E console.log(avg(4, 8));
Question 5
A function cube(n) {
B console.log(n * n * n);
C }
D const c = cube(3);

Answer Key

A function with no return gives undefined
Section A: Quick Questions
Q1
A — undefined
Q2
B — function double(n) { return n * 2; }
Q3
A — undefined
Q4
A — False: console.log only shows a value, return gives it back
Q5
B — No: it does a job, so it can skip return
Section B: Match the Pairs
Q1
function add(a, b) { a + b; } console.log(add(2, 3)); → Prints undefined, because add never uses return so it gives back nothing
Q2
let result = showTotal(5, 4); → result holds undefined when showTotal only uses console.log and has no return
Q3
A function that gives a value back to the caller must use return → function double(n) { return n * 2; }
Q4
A function that only does a job, like showing a message, can leave out return → function greet(name) { console.log('Hi ' + name); }
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
function add(a, b) { return a + b; }
Q2
function hi() { console.log("Hi"); } let x = hi(); console.log(x === undefined);
Q3
function sq(n) { return n * n; } let r = sq(3); console.log(r);
Q4
function greet() { console.log("Hi"); } let msg = greet(); console.log(msg);
Q5
function sq(n) { n * n; } let r = sq(4); console.log(typeof r);
Section E: Spot the Error
Q1
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; }.
Q2
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;
Q3
C — A return with no value gives back undefined, so the call prints undefined instead of 5. It should be return sum;
Q4
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;
Q5
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;