Saturday 19 November 2011

Javascript user defined function


If we are writing the definition for the functions, then it is called user defined functions.

We know that the main is the entry point of the program. If we want that some process is repeated again and again then writing the same code several times. The complexity of program increase and the compilation time increases. When debugging the program it will take much time. Otherwise if we divided the program into several parts, it will be much easier. The part in known as subprogram. And such subprogram is known as “function”.

          Syntax

                        function  function_name([parameter list])
                   {
                             Statement are to be written
                             [return variable name / value]
                   }

·         Function are declared and created using function keyword.
·       Function name is always starting from alphabet character. It also allowed special char_ only. Function is case sensitive.
·        Parameter passed to the function appear in parentheses and commas separated member of the list.
·        The return statement can be used to return any valid expression that evaluates a single value.
·       A function contain some code that will be executed only by an event or by a call to that function.
·         Functions are defined at the begging of a page, in the <head> section.

Ø       No argument and no return value

This type of function does not take any argument and does not have any return value.

Example

<html>
          <head>
                   <script language="javascript">
                             function display()
                             {
                                      document.write("Hello World ");
                             }
</script>
          </head>
          <body>
                   <script language="javascript">
                             display();
                   </script>
          </body>
     </html>

Ø       With argument no return value

This type of function takes some argument and does not have any return value.

Example

<html>
          <head>
                   <script language="javascript">
                             function myfun(str)
                             {
                                      document.write(str);
                             }
</script>
          </head>
          <body>
                   <script language="javascript">
                             myfun(“Hello World”);
                   </script>
          </body>
     </html>

In the above example the function is created named myfun which accepts some argument from the user and will display that variable’s value. Here the string value “Hello World” is passed to the function which is stored in the variable str and that variable’s value printed.

Ø       With argument and with return value

This type of function takes some argument and does return the value. When the value is returned by the function it is required that when the function is called the variable should be there to accept the returned value from the function.

Example

<html>
          <head>
                   <script language="javascript">
                             function add(a, b)
                             {
                                      var c;
                                      c = a+b;
                                      return c;
                             }
</script>
          </head>
          <body>
                   <script language="javascript">
                             var total;
                             total = add(20,10);
                             document.write(total);
                   </script>
            </body>
</html>

The above example has the function add which accepts two argument a and b with the value 20 and 10 respectively which are passed when function is called. After the addition the value is stored in the variable c and is returned back to the function which is called. After returning it is stored in the variable total and this variable displayed. The output will be 30.  

Posted By : Hemangi Zala

No comments:

Post a Comment