Why use functions?
You could write your whole JavaScript script in one long thing. The advantage of is that you read it from top to bottom. And if you only have a few lines of code, that isn't going to be a problem. But if you have 400 lines of code, then it gets trick...
blog.nicm42.me.uk3 min read
A function is a group of statements that together perform a specific task. You can write all intended functionality at one place and call it wherever required.
Why use function ?
Function are used for divide a large code into module, due to this we can easily debug and maintain the code. For example if we write a calculator programs at that time we can write every logic in a separate function (For addition sum(), for subtraction sub()). Any function can be called many times.
Defining a function:
return_type function_name(parameter) { function body; }Calling a function:
Advantage of Function: