JavaScript User Defined Functions: In this tutorial you will get to know about user defined functions, which are one of the major component of any programming language.
JavaScript User Defined Functions: In this tutorial you will get to know about user defined functions, which are one of the major component of any programming language.JavaScript User-Defined Functions:
An user-defined function saves us from rewriting the same code again and again and helps us to make our application smaller. The given examples will help you to understand how can you put them to work.
JavaScript has so many built-in functions, besides that you can make your own as per the need. General structure of the user defined function is:
function function_name(par1, par2,..){ statement}
par1, par2 are the name of the parameter, one function could have one, more than one or no parameter. Parameters could be of any datatype.
Example 1(Simple Function):
<html> <head> <title>Write your title here </title> <script type="text/javascript" > function display() { document.write("Hello there"); } display(); </script> </head> </html>
Output:
Hello there
Example 2 (Return a Value):
<html> <head> <title>Write your title here </title> <script type="text/javascript" > function add(x,y) { return (x+y); } </script> </head> <body> <script type="text/javascript"> var x=add(12,12); document.write("Addition is " + x); </script> </body> </html>
Output:
Addition is 24
Example 3 (Return a String):
<html> <head> <title>Write your title here </title> <script type="text/javascript" > function display() { return("Return Statement"); } </script> </head> <body> <script type="text/javascript"> document.write(display()); </script> </body> </html>
Output:
Return Statement