Added code and explanation for both the functions

Added the completed code for both functions and a detailed explanation
of the overall code as well as of each of the functions.