Function Call Statement

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email
A function is a block of statements, which is given a name. A function can be called whenever its action is required by its name. A function may or may not return a value. Before calling a function it must be declared. Functions can be defined by giving a name and the parameters it can take followed by the block of statements it has to execute.The syntax for defining a function is as follows:

function function name(parameterl, parameter 2 parameter n){
block of statements

Generally it's best to define the functions for a page in the HEAD portion of a document. Since the HEAD is loaded first, this guarantees that functions are loaded before the user might call a function. Alternately, some programmers place all of their functions into a separate file, and include them in a page using the SRC attribute of the SCRIPT tag. Either way, the key is to load the function definitions before any code is executed. The following example illustrates the use of the function call statements.

<html>
<head>
<title>My First JavaScript Program</title>
</head>

<body>
<script Language="JavaScript">
<!--
function fun(){
var a="prompt("Enter a number")
var fact=1;
for(i=1;i<=a;i++)
fat*=i
alert("The function has been cancelled")
alert("The factorial of "+a" is "+fact)
}
//-->
</script>
</head>
<body on load="fun();">
</body>
</html>
In the above example you have been introduced to two new methods, the alert() method and the promot() method. These two methods are defined in the window object. The window object is discussed in the 6 chapter. The alert() is used to display an alert box and the promot() is used to get input from the user,

Local variables in functions are declared with the keyword var preceding them. Unlike global variables their scope lies only within the function.

The return statement

A function can return a value, This is done with the help of the return statement. The syntax for the return statement is:
return expression



Domain Name Search

www.


Copyright (C) 2007. Web Domain design hosting. All rights reserved.