5.6 Function

The best way to develop and maintain large programs is to construct it from small, simple pieces, or modules.  This technique is called divide and conquer.  Modules in JavaScrit are called functions.

JavaScript programs are written by combining new functions that the programmer writes with prepackaged functions and objects available in JavaScript.

The prepackages functions that belong to JavaScript objects are often called methods.  The term methods implies that function belong to particular JavaScript object.  However, the terms function and method can be use interchangeable.  In general functions that belong to a specific JavaScript object are called methods and all others are referred to as functions.

JavaScript User-defined Functions:

The programmer can write functions to define specific tasks that may be used at may points in a script.  These functions are referred to as programmer-defined functions.  The actual statements defining the function are written only once and are hidden from functions.

A function is invoked by a function call.  The function call specified the function name and provides information (as arguments) that the called function needs to perform its task.  The functions allow programmer to modularize a program.  Most functions have a list of parameters that provide the means for communicating information between functions via function calls.  There are several reasons for modularizing a program with functions.  The divide- and-conquer approach makes program development more manageable.  Another reason is software reusability (Using the existing functions to  create new programs)


Function functionname(var1, var2)
Function codes are entered here


Var1,var2, etc are variable or values passed into the function.  The { and the } defines the start and end of the function.

Note: A function with no parameters must include the paratheses () after the function name:


<script type = “text/javascript”>
Function displaymessae()
Alert (“Hello World”);
<input type =”button” value = “Click me!”
Onclick = “displaymessage()”>


If the line: alert (“Hello World!!”) in the example above had not been put within a function, it would have been executed as soon as the line was loaded.  Now, the script is not executed before the user hits the button.  We have added an on Click event to the button that will execute the function displaymessage() when the button is clicked.

<title> script user function </title>
<script type =”text/JavaScript”>
Document.write (“<h1> square the numbers from 1 to 10 </h1>”);
For (var x=1; x<=10; ++x)
Document.writeIn (“ the square of “ +x “is” + square(x) +”<br/>”);

Function square(y)
return y*y;
</script> </head> <body>
<p> click refresh </p>
</body> </html>


The return Statement

The return statement is used to specify the value that is returned from the function.  So, functions that are going to return a value must use the return statement.  In above program the return statement in function square passes the calculation.

Leave a Reply

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

  • Get Alert

    Enter your email address:

  • Categories

  • Twitter

Subscribe Now!
Sign-up for our email newsletter and get free job alerts, current affairs and GK updates regularly.

Subscribe Here

Popup Dialog Box Powered By : XYZScripts.com
  • RSS
  • Facebook
  • Google+
  • Twitter