A function is a reusable block of code with a name. You write it once, then call it as many times as you need.
Declaring and calling a function
function direBonjour(prenom) {
console.log("Bonjour " + prenom);
}
direBonjour("Marie");
Here, prenom is a parameter: a piece of information the function receives in order to do its job.
Returning a value
A function can send back a result with the return keyword:
- The result can be stored in a variable.
- Once
returnruns, the function stops.
function addition(a, b) {
return a + b;
}
let total = addition(3, 4); // 7
Functions keep code organised and avoid repetition: a change in one place takes effect everywhere the function is called.
Key takeaway: a function bundles reusable code. It takes parameters and can send back a result with
return.