Functions

Say you are pricing items in a cart. You take a number, add tax, round it, and format it with a currency symbol. Then you need to do it again for the next item, and the next. Copying those four steps everywhere is how bugs get in: fix the rounding in one place and you have three more copies still wrong. A function lets you name that block of steps once and run it whenever you need it. This chapter is about writing those blocks, feeding them inputs, and getting values back out.
Declaring and calling a function
A function is a named block of code you can run whenever you want. You write the steps once, give them a name, and then run them by that name as many times as you like.
You make one with the function keyword:
function greet() {
console.log("Hi there");
}That is the whole definition, but nothing has happened yet. Writing a function stores the steps; it does not run them. To actually run it, you call it by writing its name followed by parentheses:
greet(); // Hi there
greet(); // Hi thereTwo lines, two greetings, from one block of code. Defining and calling are separate things, and mixing them up is the first thing that trips people.
Give a function a clear, verb-like name for what it does (greet, logDivider), the same care you take when naming variables. A function you call in ten places is only as readable as its name.
greet(). That split confused me at first: the definition is the recipe, the call is actually cooking. You can call the same function as many times as you want. Parameters and return values
A function is more useful when you can feed it something. The values you feed in are parameters: names listed in the parentheses that stand in for whatever you pass when you call it.
function greet(name) {
console.log("Hi " + name);
}
greet("Sam"); // Hi Sam
greet("Priya"); // Hi Priyaname is the parameter. When you call greet("Sam"), the text "Sam" slots into name for that run. One function, different results, depending on what you hand it.
So far these functions print something. Often you want a function to hand a value back instead, so you can use it later. That is what return does:
function double(number) {
return number * 2;
}
const result = double(5);
console.log(result); // 10return sends a value out of the function. double(5) becomes 10, and you can store it, print it, or pass it on. Printing shows a value on screen; returning gives it back to your code to keep using.
return hands a value back so your code can use it later. Printing with console.log shows it on screen; returning gives it back to you. Took me a while to feel that difference. Arrow functions
There is a shorter way to write a function, called an arrow function. You store it in a variable and use the => arrow instead of the function keyword:
const add = (a, b) => {
return a + b;
};
console.log(add(2, 3)); // 5When the function is a single expression like this, you can drop the braces and the return and let the arrow hand the value back for you:
const add = (a, b) => a + b;
console.log(add(2, 3)); // 5Both do the same thing. You will see arrow functions everywhere in modern JavaScript, so it helps to recognise the shape: a name, an =, the inputs, an arrow, and the body.
=> instead of the function keyword. When the body is one expression, you can drop the braces and return and the arrow hands the value back for you. You will see this shape constantly, so getting comfortable reading it pays off fast. Where functions go from here
Functions are how you stop repeating yourself: name a block of steps, feed it inputs, get a value back. Once that clicks, most of the code you write becomes small functions calling other functions, each doing one clear job.
From here, functions show up everywhere. You will pass them to array methods to transform lists of data, use them to organise the branching logic from conditionals, and lean on the naming discipline you started with variables. The next time you catch yourself copying a block of code, that is the signal to wrap it in a function instead.

