Return Values

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — Getting a Result Back

So far your functions print things. But often you want a function to compute something and return the result to you. That's what `return` does:

function double(value) { return value * 2; } let damage = double(15); console.log(damage);

The call `double(15)` is replaced by the value the function returns: 30. You can store it, print it, or use it in a calculation.

function double(value) { return value * 2; } console.log(double(5)); console.log(double(5) + double(10));

Exercise 2 — Printing vs Returning

`console.log` shows a value on screen. `return` gives a value back to the code that called the function. They're not the same thing. A function without `return` returns `undefined`:

function printDouble(value) { console.log(value * 2); } let result = printDouble(4); console.log("Result: " + result);

The 8 is printed by the function itself, but `result` holds `undefined` because nothing was returned. With `return`, the value can be reused:

function double(value) { return value * 2; } let result = double(4); console.log("Result: " + result);

Exercise 3 — Quiz: The Returned Value

What does this code print?

function add(a, b) { return a + b; } let total = add(2, 3); console.log(total);
Options
• 5
• undefined
• a + b
• 23

Hint : The call becomes the value written after the keyword.

Exercise 4 — Multiply

Complete the function so it returns the product of its two parameters.

Hint : The value must come back to the caller, and a product is a specific operation.

Exercise 5 — return Stops the Function

As soon as `return` runs, the function ends immediately. Any code after it is never executed:

function getBonus() { return 50; console.log("This line never runs"); } console.log(getBonus());

This is useful with conditions: return early in one case, continue in the others.

function describe(hp) { if (hp <= 0) { return "KO"; } return "still fighting with " + hp + " HP"; } console.log(describe(0)); console.log(describe(35));

Exercise 6 — Quiz: After return

What does this code print?

function rank(level) { if (level >= 10) { return "Elite"; } return "Rookie"; } console.log(rank(12));
Options
• Elite
• Rookie
• Elite then Rookie
• undefined

Hint : Once a value is returned, the rest of the body is skipped.

Exercise 7 — Returning Booleans

A function can return `true` or `false`. It then reads like a question, and fits directly in a condition:

function isAlive(hp) { return hp > 0; } console.log(isAlive(20)); console.log(isAlive(0));function isAlive(hp) { return hp > 0; } let pyrochuHP = 0; if (isAlive(pyrochuHP)) { console.log("Pyrochu attacks!"); } else { console.log("Pyrochu can't fight anymore"); }

Naming such functions `isSomething` or `canSomething` makes the condition read like plain English.

Exercise 8 — Can It Evolve?

A Codémon can evolve from level 20. Complete the function and use it in the condition.

Hint : "From level 20" includes 20 itself. Then the condition just needs the question asked.

Exercise 9 — Results Inside Results

Since a call becomes a value, you can pass it straight to another function, or combine several calls in one expression:

function baseDamage(level) { return level * 3; } function withBonus(damage) { return damage + 10; } let final = withBonus(baseDamage(5)); console.log(final);

`baseDamage(5)` gives 15, which becomes the argument of `withBonus`, which gives 25. Read nested calls from the inside out.

Exercise 10 — Quiz: Nested Calls

What does this code print?

function half(x) { return x / 2; } function plusOne(x) { return x + 1; } console.log(plusOne(half(10)));
Options
• 6
• 5.5
• 11
• undefined

Hint : Compute the innermost call first, then feed its result outward.

Exercise 11 — return Cheat Sheet

Match each element with its effect.

Items to match
• return x;
• A function without return
• Code written after return
• let r = f();
Definitions
• Sends x back and ends the function
• Returns `undefined`
• Never runs
• Stores the returned value

Exercise 12 — Damage Calculator

Build two small calculation functions and use them together. 1. `damage(base, level)` returns the base damage plus twice the level 2. `remainingHP(hp, dmg)` returns the HP left after the hit, never below 0 3. Pyrochu has 50 HP and takes a base 20 attack from a level 12 Codémon: print `"Pyrochu takes X damage, Y HP left"` using both functions

Starter code
// Write your code here
Back to the course