Using and Calling Functions
So the 3 examples in Understanding Functions were defining functions, meaning they created functions that can be used later on. Let's see how we can use them in code.
const answer = add(2, 2);
log("answer", answer); // [answer] 4Is it that simple? Yes, it really is. Let's break down a few things before we continue.
function functionName(argument, otherargument) {
  //function code
  return "Some Value";
}- functionis the keyword that defines a function. There are other ways to define them, but this is the "classical" way.
- functionNameis the name of the function itself, how it will be called. so- function add()will be called as- add()
- argumentand- otherargumentare, of course, called the "Arguments" for the function. An Argument's name must not be confused with the Parameters that are sent to the function. That is to say, in the- add()function,- aand- bare the arguments and- 2and- 2are the parameters sent to it.
- returnis a special keyword that does two very distinct things: first, it returns a value that's after it. So,- return 2;will return the integer- 2to whatever called that function. Second, it stops the execution of the function. This means that anything after a return keyword will not execute. This is useful if you want to stop execution based on a condition. For example:
// Stop execution if either arguments are missing: 
function add(a, b) {
  if(!a || !b) return false;
  return a + b;
};The above will return false if either the a or b parameter is missing. The a+b line will simply never even be executed, which can save a lot of processing power in larger code.
Optional Arguments
In the previous examples, a and b were required arguments, meaning that you could not call the add function without both arguments being present (otherwise, they would be null and our calculation would fail). You can, however, give a function optional arguments with a default value assigned. 
Let's say we want add() to increment by 1 only, if the second value is not present, instead of stopping the function. This is done by assigning a default value to b when we create the function: 
function add(a, b = 1) {
  // We're still stopping the function if a is missing)
  if(!a) return false; 
  return a + b;
}
console.log(add(5)); // 6Last updated
