JavaScript – Functions

A function is a group of reusable code which can be called anywhere in your program. This eliminates the need of writing the same code again and again. It helps programmers in writing modular codes. Functions allow a programmer to divide a big program into a number of small and manageable functions.

Like any other advanced programming language, JavaScript also supports all the features necessary to write modular code using functions. You must have seen functions like alert() and write() in the earlier chapters. We were using these functions again and again, but they had been written in core JavaScript only once.

JavaScript allows us to write our own functions as well. This section explains how to write your own functions in JavaScript.

Function Definition

Before we use a function, we need to define it. The most common way to define a function in JavaScript is by using the function keyword, followed by a unique function name, a list of parameters (that might 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 sayHello that takes no parameters −

<script type="text/javascript">

      function sayHello()
      {
         alert("Hello there");
      }

</script>

Calling a Function

To invoke a function somewhere later in the script, you would simply need to write the name of that function as shown in the following code.

<html>
   <head>
   
      <script type="text/javascript">
         function sayHello()
         {
            document.write ("Hello there!");
         }
      </script>
      
   </head>
   <body>
      <p>Click the following button to call the function</p>
      
      <form>
         <input type="button" onclick="sayHello()" value="Say Hello">
      </form>
      
      <p>Use different text in write method and then try...</p>
   </body>
</html>

Function Parameters

Till now, we have seen functions without parameters. But there is a facility to pass different parameters while calling a function. These passed parameters can be captured inside the function and any manipulation can be done over those parameters. A function can take multiple parameters separated by comma.

Example

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

<html>
   <head>
   
      <script type="text/javascript">
         function sayHello(name, age)
         {
            document.write (name + " is " + age + " years old.");
         }
      </script>
      
   </head>
   <body>
      <p>Click the following button to call the function</p>
      
      <form>
         <input type="button" onclick="sayHello('Zara', 7)" value="Say Hello">
      </form>
      
      <p>Use different parameters inside the function and then try...</p>
   </body>
</html>

The return Statement

A JavaScript function can have an optional return statement. This is required if you want to return a value from a function. This statement should be the last statement in a function.

For example, you can pass two numbers in a function and then you can expect the function to return their multiplication in your calling program.

Example

Try the following example. It defines a function that takes two parameters and concatenates them before returning the resultant in the calling program.

<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 following button to call the function</p>
      
      <form>
         <input type="button" onclick="secondFunction()" value="Call Function">
      </form>
      
      <p>Use different parameters inside the function and then try...</p>
  
  </body>
</html>
 

Related Posts

  • 44
    Page redirection is the method or technique which we can transfer the values and page source to destination.this this vary simple. You are also use to place a "redirect page" at the old location which, after a timed delay, will forward visitors to the new location of your webpage. You…
    Tags: function, javascript
  • 42
    This section will provide you with the basics of what JavaScript is, and why you should use it. Objectives     JavaScript versus JAVA     Interpreted programs versus Compiled programs     Why JavaScript     What you can use JavaScript for     About JavaScript JavaScript is a lightweight, interpreted programming language. It…
    Tags: javascript
  • 42
    Let us take a sample example to print out "Hello World". We added an optional HTML comment that surrounds our JavaScript code. This is to save our code from a browser that does not support JavaScript. The comment ends with a "//-->". Here "//" signifies a comment in JavaScript, so…
    Tags: javascript
  • 41
    JavaScript Variable Scope The scope of a variable is the region of your program in which it is defined. JavaScript variables have only two scopes. Global Variables− A global variable has global scope which means it can be defined anywhere in your JavaScript code. Local Variables− A local variable will…
    Tags: javascript, function
  • 41
    JavaScript Datatype One of the most fundamental characteristics of a programming language is the set of data types it supports. These are the type of values that can be represented and manipulated in a programming language. JavaScript allows you to work with three primitive data types − Numbers, 123, 120.50…
    Tags: javascript

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: