Syncing account progress…

Back

Beginner · 8 min

Reusable functions

Declare a function with parameters and return a calculated value.

Name a reusable operation

A function groups instructions you can call again. Parameters are names for the values passed into a call. return sends a result back to the caller.

function double(value) {
  return value * 2;
}
console.log(double(4));
console.log(double(7));

Output

8
14

Use the returned value

A function can return a value without displaying it. Store that result or pass it to another expression. A variable declared inside a function belongs to that function.

function cost(price, quantity) {
  const total = price * quantity;
  return total;
}
const result = cost(5, 3);
console.log(result);

Output

15

Put it into practice

  1. Predict the output of each example before running it.
  2. Complete the coding task, then try the practice questions.

Try it yourself

Code runs on this device. When you are signed in, drafts sync to your account.

Return a delivery total

Make totalWithDelivery return subtotal plus fee. Display the result for 20 and 3.

Practice