Skip to content
WANCODE
BM

Functions

15 minBeginnerHTML · CSS · JS

In this lesson

  • Declare and call a function with parameters
  • Explain the difference between return and console.log
  • Write arrow functions
  • Know which variables a function can see (scope)

The big idea

A function lets you group instructions under a name and run them whenever you need. Give it inputs, it gives you back an output — like a machine.

You've already used functions: console.log(...), Math.round(...), name.trim(). Somebody wrote those once, and now everyone can call them. Now you'll write your own.

Inputs go in, the function body runs, one output comes back out.
JavaScript
// declare: the recipe (nothing runs yet)
function teaPrice(cups, pricePerCup) {
  return cups * pricePerCup;
}

// call: actually run it, with real values
const total = teaPrice(3, 2.8);   // 8.4 (well, 8.399999999999999 — remember lesson 3!)
console.log(teaPrice(1, 2.8));    // 2.8
Parameter
The placeholder name in the declaration: cups, pricePerCup. Like a variable that only exists inside the function.
Argument
The actual value you pass when calling: 3 and 2.8. Arguments fill parameters in order.
return
Hands a value back to whoever called the function, and stops the function immediately.

return vs console.log

This is the most common beginner mix-up. console.log shows a value to you, the developer — then the value is gone. return gives the value back to your code, so it can be stored, used in maths, or passed to another function. A function with no return gives back undefined.

Only prints

JavaScript
function double(n) {
  console.log(n * 2);
}
const x = double(5); // prints 10
console.log(x + 1);  // NaN — x is undefined!

Returns

JavaScript
function double(n) {
  return n * 2;
}
const x = double(5); // x is 10
console.log(x + 1);  // 11

Arrow functions

Arrow functions are a shorter way to write a function, stored in a const. If the body is a single expression, you can drop the { } and the return — the value is returned automatically.

JavaScript
// all three do the same thing
function square(n) {
  return n * n;
}

const square2 = (n) => {
  return n * n;
};

const square3 = (n) => n * n; // short form: implicit return

Scope: who can see what

JavaScript
const shop = "Kedai Kopi WAN";     // outside: visible everywhere below

function receipt(amount) {
  const line = shop + ": RM" + amount; // inside can see outside ✔
  return line;
}

console.log(receipt(5));
console.log(line); // ReferenceError: line is not defined ✘

Variables created inside a function (including its parameters) live only inside its { } — that's its scope. Code inside can read variables from outside, but not the other way round. This keeps functions tidy: their working-out doesn't leak into the rest of your program. Prefer passing values in as parameters rather than reaching for outside variables.

The console prints the right number, then With service charge: RMundefined. Explain to yourself why, then fix addServiceCharge so it returns the price plus 10%. Round the result to 2 decimal places with the Math.round(x * 100) / 100 trick from lesson 3, so addServiceCharge(12.5) gives exactly 13.75 (not 13.750000000000002).

Checks

  • addServiceCharge returns a number
  • Adds 10% and rounds to 2 decimals
  • The console no longer shows RMundefined

Check understanding

Q1

function greet() { console.log("Hai"); } — what is const x = greet();?

Q2

In function area(w, h) { return w * h; } and area(3, 4), what are 3 and 4?

Q3

A variable total is created with const inside a function. Can code outside the function use total?

What you learned

  • Declare with function name(params) { ... } or const name = (params) => ...; call with name(args).
  • return hands a value back to your code; console.log only shows it to you. No return means undefined.
  • Handle edge cases first with an early return.
  • Variables inside a function stay inside (scope). Pass data in with parameters and get it out with return.
Preview

Console output from your code appears here.

Step 1 of 3