Codeloft
0 0 XP

Functions

7 min 20 XP + 10 for no hints

A function is a recipe you can reuse. It takes inputs called parameters and sends back a result with return.

function add(a, b) { return a + b; } defines it. add(2, 3) calls it and gives back 5.

The checks call your functions with different numbers, so they test that your code works, not that it matches one exact answer.

Example
function greet(name) {
  return "Hi, " + name + "!";
}

console.log(greet("Asha"));
console.log(greet("Leo"));
Without return, a function gives back undefined. The checks will tell you if that happens.

Your tasks

0 of 3 done
  1. add(2, 3) returns 5Expected 5, got undefined
  2. add(-4, 10) returns 6Expected 6, got undefined
  3. Write double(n), which returns n times 2You used "double", but the computer doesn't know that name yet. Check the spelling (capital letters count), or create it first with let.

Quick check

What does a function give back if it has no return?

math.jsPress Run or Ctrl + Enter
undefined