Summary of “Functions”. Part 1

Function example.

var calculateSum = function (numberFirst, numberSecond) {
  var sum = numberFirst + numberSecond;
  return sum;
};

calculateSum(); // Will return NaN
calculateSum(2); // Will return NaN
calculateSum(2, 5); // Will return 7
calculateSum(9, 5); // Will return 14

In this example:

  • calculateSum is the name you can use to call the function.
  • numberFirst, numberSecond are function parameters.
  • return sum; is the place in the code where sum is returned and where we exit the function.
  • calculateSum(2, 5); are arguments that are transferred in the function when it is called. The order of the arguments is the same as for the function parameters. The first argument 2 is written to the first parameter numberFirst, argument 5 is written to parameter numberSecond. It’s important to observe the parameter order when calling the function in order to avoid errors that are not obvious.

Continue