StreetDocsPlayground
StreetDocs/Make Your Web Page Smart/Functions — Reusable Steps
javascript
14 minLearning in Pidgin

Functions — Reusable Steps

Package Ada's calculation inside a function wey she fit call anytime.

What you'll learn

  • Create a function
  • Use parameters and return
  • Call a function

Before you start

  • making-decisions

Ada dey add prices many times every day. Instead of writing di same thing again and again, she fit package am inside a function — one reusable box of steps.

Create a function

function addPrices(priceA, priceB) {
  let total = priceA + priceB;
  return total;
}
  • priceA and priceB na parameters — di inputs.
  • return dey send di result back to whoever call am.

Call di function

let todayTotal = addPrices(850, 450);
console.log(todayTotal);

Now, anytime Ada get new prices, she just call addPrices again. One small step wey you fit use many times — na so code stay clean and no dey repeat.

Try it yourself

Reusable addition

Open in playground →
javascript
function addPrices(priceA, priceB) {
  return priceA + priceB;
}
let todayTotal = addPrices(850, 450);
console.log(todayTotal);
Output:
1300

Practice exercises

Exercise 1: Your own function

Create a function 'addPrices' wey dey add two numbers and return di result. Call am with 5 and 3, then print di result.

Preview
Console

No output yet. Click Run.

Code runs in your browser only — nothing is sent to the server.