JavaScript Functions

A function is a set of statements, which perform a specific task. These functions are reusable and can be called anywhere in your program.

Functions allow a programmer to divide a big program into a number of small and manageable functions.

For example to design a program of calculator in spite of designing whole thing from starting , it is good to use small module like addition , subtraction , square root etc.

There are two type of functions in JavaScript:

  • Built-in Functions
  • User Define Functions

Built-in function are that functions which are predefined and supplied along with the compiler.

Programmer can also define functions to do a specific task relevant to their programs. Such functions are called user-defined functions. In this topic we will discuss user-defined functions.

Function Definition: Before using any function , we have to define it. We need to tell compiler about the name , return type and argument list of function.

Syntax: Following is syntax of function:

<script type = "text/javascript">
   <
      function functionname(parameter-list) {
         statements
      }
   >
</script>

Explanation:

  • ‘function’ is a keyword, which tell the compiler that one function is defined.
  • ‘functionname’ will be the name given by programmer.
  • Parameter-list is list of arguments which will pass during calling.

Example:

<script type = "text/javascript">
   <
      function first()
        {
         alert("Hello JavaScript");
        }
   >
</script

Explanation: Here we define a function named first, and it has single print statement.

Function Calling: We can call or invoke the function anywhere in program by simply  to write the name of that function.

<html>
  
   <body>
         <script type = "text/javascript">
          function first1()                      //function define
          {
            document.write ("Hello javaScript");
          }
            first1();                           //function calling
         </script>
   </body>
</html>

Explanation:

  • Here we define a function named first1().
  • This function has one write statement.
  • We simply write the name of function and put round braces, this is the syntax to call or invoke any function.

Function Parameter: We can pass arguments into function. When we define a function we pass the list of parameters. When we call the function we pass the real values that are known as arguments. Syntax is as following:

function functionName(parameter1, parameter2, parameter3) 
{
  // code to be executed
}

Rules:

  • In JavaScript during function definition we need not to specify data-type parameter.
  • In JavaScript functions do not perform type checking on the passed arguments.
  • In JavaScript functions do not check the number of arguments received.

Example:

<html>
   <head> </head>
<body>   
      <script type = "text/javascript">
         function info(name, age) 
         {           
            document.write (name + " is " + age + " years old.");
                 }
     info("easecod",2);
      </script>      
   </body>
</html>

Explanation: 

  • We define a function named info and pass two parameters(name,age).
  • There is single print statement.
  • We call info function and pass two arguments (string,integer).
  • name=”easecod” , age=2.

Output:

easecod is 2 years old.