In javascript functions usually used for performing actions or tasks. For executing code present in a function some one must explicitly call the function.

Note: Javascript code is not self executable since javascript will not contain main method.

Function Syntax:

function function-name(parameter1,parameter2,...)
{
    //set of javascript statements or code.
    //use return statement if you want to return any data.
}

Return statement:

Use return keyword for returning data from function.

Calling Javascript function:

Java script functions can be called from other javascript function or from an html event or can be self invoked.

Example for Calling Javascript from another function:

<script language="javascript" type="text/javascript">

      function greetings(message) {
          return 'hello'+message;
      }
        function fnTest() {
         //The following code calls greetings function and variable msg stores output of function.
         var msg=greetings('skillgun');
         alert(msg);
        }

</script>

Example for calling Javascript from HTML event:

In the following code greetings javascript function is executed when user clicks on html button (or when onclick event is fired) or when user moves mouse on top of messages dropdown list.
  <html>
    <head>
   <title>Calling Javascript Function</title>
   <script language="javascript" type="text/javascript">
     function greetings(message) {
       return 'hello'+message;
     }
     </script>
     </head>
     <body>
     <input type="submit" value="submit" onclick="greetings('palle')"/>
     message:<select onmouseover="greetings('palle')">
     <option>Greeting first</option>
     <option>Greetings second</option>
     </select>
     </body>
   </html>

Example for calling a Javascript from itself:

In the following sample code greetings function executes or calls itself.
      <script language="javascript" type="text/javascript">

        function greetings(message) {
         
         alert(message);
         greetings('skillgun');
        }

     </script>
Note: In javascript functions are considered as objects and hence we can assign a function to a variable.
     <html>
     <head>
     <title> Assigning a Function to a variable</title>
     <script language="javascript" type="text/javascript">
      var add=function (a,b) {
        alert(a+b);
      }
      function test() {
       add(10,20);//this code will call function which is present in add variable.
      }
      </script>
      </head>
      <body>
       <input type="submit" value="Add" onclick="test()"/>
      </body>
      </html>
Note: Passing values to the java script function parameters is not mandatory. Hence the following code will not produce any error and the second parameter will get undefined as value.
      <html>
      <head>
      <title>Assigning a Function to a variable</title>
      <script language="javascript" type="text/javascript">
       function myFunction(parameter1,parameter2) {
        if (parameter2==undefined)
          alert('only one value passed');
        else if (parameter1==undefined && parameter2==undefined)
          alert('both values are parameter');
        }
       </script>
       </head>
       <body>
       <input type="submit" value="submit" onclick="myFunction('skill')"/>
       </body>
       </html>