javascript function

javascript function

javascript function

A function is a set of reusable program which can be known as

Anywhere in your app.  This removes the need for writing the identical code

over and over.  It assists programmers in writing codes that are modular.  Functions allow

a developer to divide a big program into a number of both small and manageable

purposes. 

Like every other advanced programming language, JavaScript additionally

Supports all the characteristics necessary to write modular code using functions.  You

must have seen features such as alert() and compose () in

the previous chapters.  We were using these functions over and over, but they

were written in center JavaScript merely once. 

JavaScript allows us to write our functions as well.  This

The section explains how to write your functions in JavaScript.A service is a group of reusable code which can be called

anywhere in your program.  This eliminates the need of writing the identical code

again and again.  It helps programmers in modular writing PROGRAM.  Functions allow

a programmer to divide a big CODE into several small and manageable

purposes. 

Like every other advanced programming language, JavaScript also.

Supports all the characteristics necessary to write modular code using functions.  You

have to have seen features such as alert() and write() from the previous chapters.  We had been using those functions again and again, but they

had been composed in center JavaScript merely once. 

 This

The section explains how to write your functions in JavaScript.

Function Definition

Before we use a function, we

Must define it.  The most typical approach to identify a specific purpose in JavaScript is by

using the function keyword, followed with a unique feature

name, a list of parameters (that may be empty), and a statement block

surrounded by curly braces.

Syntax

The basic syntax is shown here.

<script type = “text/javascript”>

   <!–

      function functionname(parameter-list) {

         statements

      }

   //–>

</script>

Example

Try the following example. It defines a function called say that takes no parameters −

<script type = “text/javascript”>

   <!–

      function say() {

         alert(“Hello there”);

      }

   //–>

</script>

Calling a Function

To invoke

a function somewhere later in the script, then you’d just must compose the

title of that purpose as revealed in the following code.

<html>

   <head>  

      <script type = “text/javascript”>

         function say() {

            document.write (” there!”);

         }

      </script>

   </head>

   <body>

      <p>Click the following button  the function</p>     

      <form>

         <input type = “button” onclick = “say()” value = “Say “>

      </form>     

      <p>different text in write method and then try…</p>

   </body>

</html>

Function

Till now, we’ve seen works without parameters.  But there is

a facility to pass parameters while calling a function.  These passed

parameters could be recorded inside the role and any manipulation can be performed over those parameters.  A function may take multiple parameters separated by

comma.

Example

Try the following example. We have modified our sayHello function here. Now it takes two parameters.

<html>

   <head>  

      <script type = “text/javascript”>

         function say(name, age) {

            document.write (name + ” is ” + age + ” years “);

         }

      </script>     

   </head>

   <body>

      <p>Click the  button to call the function</p>     

      <form>

         <input type = “button” onclick = “say(‘Zara’, 7)” value = “Say “>

      </form>     

      <p>Use different inside the function and then try…</p>

   </body>

</html>

The return

A JavaScript function could have a optional yield  statement.

This is required if you would like to return a value from a job.  This announcement should be the last statement in a function. 

For example, you can

Pass two amounts at a function, and then you can expect the work to return

Their multiplication on your calling program.

Example

Try the following example. It a function that takes two parameters and conc ates them before returning the resultant in the calling code.

<html>

   <head> 

      <script type = “text/javascript”>

         function concatenate(first, last) {

            var full;

            full = first + last;

            return full;

         }

         function secondFunction() {

            var result;

            result = concatenate(‘Zara’, ‘Ali’);

            document.write (result );

         }

      </script>     

   </head>

   <body>

      <p>Click the button to call the function</p>     

      <form>

         <input type = “buton” onclick = “secondFunction()” value = “Call Function”>

      </form>     

      <p>Use different parameters inside the function and then try…</p> 

  </body>

</html>