A functions is a set of statements or a block of code.It is executed when it is called or by an event.
A function can be reused in the script as many times as you wish.
These functions are written in Core JS and used by us.
We use function to prevent the browser from executing script at the time of page load.
With the help of a function we can execute script after an event occurs such as onclick,onhover,onfocus etc.
Function can be run in two ways:
- With the help of event such as onclick,onfocus.
- From another function
Creating a Function
To create a function type funcion followed by the name you wish for your function and then the parenthesis "()".
These parenthesis include the parameters of your function.
Here is an example.
Function With No Parameters
We can pass various number of arguments in the parenthesis of a function.Arguments are variables,
either numbers or strings, with which the function is supposed to do something.
But to begin with we will leave it blank for now.Have a look:
Click on the button.
Function with Parameters
In the above example we left the parenthesis of Show() function blank.Now we will learn what are the
different arguments or parameters we can pass in it.
Another thing that a function can do is return a value.The return statement
returns a value back to the function.
Note:After the return statement the function stops and it does'nt execute the lines below it.