return versus console.log inside a function

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

return versus console.log inside a function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. function add(a, b) { console.log(a + b); } let answer = add(2, 3); What is stored in answer?
A 5
B undefined
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); }
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
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

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

Definitions

return

Fill in the Blanks

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

1.
function double(n) { ______ n * 2; }
2.
function square(n) { return n * n; } let result = ______(4); console.log(result);
3.
function show(n) { console.log(n * 2); } let x = show(5); console.log(x); // _________
4.
function add(a, b) { return a + b; } let sum = add(2, 3); ___________(sum);
5.
function getAge() { ______ 13; } let age = getAge() + 1; console.log(age);

Spot the Error

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

Question 1
A function add(a, b) (
B return a + b;
C }
D let total = add(2, 3);
E console.log(total);
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 getGreeting(name) {
B 'Hi ' + name;
C }
D let msg = getGreeting('Sam');
E console.log(msg);
Question 4
A function area(w, h) {
B let a = w * h;
C console.log(a);
D }
E let size = area(3, 4);
Question 5
A function isEven() {
B return n % 2 === 0;
C }
D console.log(isEven(6));

Answer Key

return versus console.log inside a function
Section A: Quick Questions
Q1
A — Hands total back to the code that called the function
Q2
B — undefined
Q3
A — function double(n) { return n * 2; }
Q4
B — Shows the value in the console but does not give it back
Q5
A — Use return to give back the price
Section B: Match the Pairs
Q1
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.
Q2
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.
Q3
To save a function's answer in a variable, the function must return the value. → let total = add(2, 3); // add uses return
Q4
If you only want to display a message for the user to read, console.log is enough. → function showScore(score) { console.log(score); }
Q5
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.
Q6
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)).
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
function double(n) { return n * 2; }
Q2
function square(n) { return n * n; } let result = square(4); console.log(result);
Q3
function show(n) { console.log(n * 2); } let x = show(5); console.log(x); // undefined
Q4
function add(a, b) { return a + b; } let sum = add(2, 3); console.log(sum);
Q5
function getAge() { return 13; } let age = getAge() + 1; console.log(age);
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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;
Q4
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.
Q5
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) {