Functions
A function is a named recipe. You write it once and use it as often as you like:
function double(n) {
return n * 2;
}
console.log(double(4)); // 8
console.log(double(10)); // 20
nis a parameter: a placeholder for the value you pass in.returnsends a result back to whoever called the function, and stops the function.double(4)calls the function with4asn.
Functions can take several parameters, separated by commas:
function fullName(first, last) {
return `${first} ${last}`;
}
return is not the same as console.log
console.log only shows a value. return gives it back so the rest of the program can use it: store it, compare it, pass it on. The checks for this lesson call your functions and look at what they return.
Your task
Write three functions:
greet(name)returnsHello, <name>!, for examplegreet("Tariro")gives"Hello, Tariro!".area(width, height)returns the area of a rectangle.isEven(n)returnstrueifnis even, otherwisefalse.