Functions
15 minBeginnerHTML · CSS · JS
In this lesson
- ▸Declare and call a function with parameters
- ▸Explain the difference between
returnandconsole.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.
// 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:
3and2.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
function double(n) {
console.log(n * 2);
}
const x = double(5); // prints 10
console.log(x + 1); // NaN — x is undefined!Returns
function double(n) {
return n * 2;
}
const x = double(5); // x is 10
console.log(x + 1); // 11Arrow 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.
// 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 returnScope: who can see what
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
addServiceChargereturns a number- Adds 10% and rounds to 2 decimals
- The console no longer shows
RMundefined
Check understanding
What you learned
- ✓Declare with
function name(params) { ... }orconst name = (params) => ...; call withname(args). - ✓
returnhands a value back to your code;console.logonly shows it to you. Noreturnmeansundefined. - ✓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.
Console output from your code appears here.