Functions Calling Other Functions

Functions can call other functions

  • Function reusability is key results in cleaner code

  • Reusing functions leverages key programming principle - Don’t Repeat Yourself

  • In the example of below, the surfaceAreaOfCube function calls another function (areaOfSquare) instead of duplicating work that was already done


// Function that calculates area of a square

function areaOfSquare(side){
  return side * side;
};

areaOfSquare(3); // returns 9


// This is a function that calculates the 
// surface area of a cube that *reuses* the areaOfSquare function

function surfaceAreaOfCube(side){
  return 6 * areaOfSquare(side);
};

surfaceAreaOfCube(7); // returns 42

JS Bin on jsbin.com