What are functions?

As programming developed over the years, people realised the need to give a feature to programming languages in which they could get a block of code to be executed only when it is asked to be. The block can be reused, and thus used to carry out common procedures.

And hence functions were born!

Functions are blocks of code that get executed when they are called - implicitly or explicitly.

So, in simple terms, functions just serve to gather code in one place that can be called multiple times at will.

For an example if we have an online store program in JavaScript, we may need to calculate the total cost of the products chosen when the user clicks a Proceed to Cart button. The code for the calculation part can be grouped within a function which can then be easily called at the click of the button. And in this way we are able to execute code when its desired action happens.

There are thousands of instances when we need to execute code when something happens or just want to seperate different tasks under different names. Functions are the best deal for this.

In the coming chapters you will be able to relate pieces of this information to actual code and thus better understand all this stack of detail about functions. Let's begin with function creation first..

Don't think functions are difficult! They are very easy and a lot of fun in programming.