H I O X INDIA
Javascript Tutorial
Google
Web hscripts.com
 HOME  ||  Scripts  ||  Purchase  ||  Tutorials  ||  Images  ||  Tools  ||  Directories 
  :-)  Send Page   :-)   Feedback   :-)   Register   :-)   Links   :-)   Support   :-)   Bookmarks :-)  
 Forums   Hosting   Internet Stats   Easy Calculation   FUN Games 

Java-Script Tutorial
Introduction
Variables
Operators
Statements
Loops
Functions
Objects
Arrays
Browser Objects
Form DOM
Ask Your Doubts
Feedback





Function - Returning Value


Topic

Returning a value from a function!
How to get the result of a function to be used in other operations?



Explanation

Function - Returning a value:
Syntax:
function name(parameter 1,parameter 2,...)
{
     // set of statements that will be executed
return thevalue;
}


        A function can also return a value after doing the specified operations. To explain, we would consider a requirement where we have to calculate x2/2.
We will calculate x2 in a function and return the result of x2. After getting the return value, we will divide it by 2.

Example Code:
<script language="javascript">

function square(number1)
{
     var c = number1*number1;
     // Now we will return the result
     return c;
}

var x = 4;
// Here we invoke the function and capture the result
var des = square(x);
var res = des/2;
document.write(" The result - "+res);
</script>


Result:

In the above example we calculate x2 in the function "square(xxx)". We returned the result.

Getting the Result:
As the function is to return a value, while we invoke the function we assigned a variable to capture the result as "var des = square(xxxx);".
Now the result of x2 is assigned to the variable des.
Using the variable 'des' further operations were completed.

Note: Once the return statement is called in a function it will break, i.e no further statements below it will be executed.


        Javascript (JS) is one of the most used languages in the world. Some times spelled as Java Script. Hope you enjoy this tutorial. Do send your feedback or suggestions on this javascript or java script tutorial. This is a copyright content.

others

privacy policy     license     sitemap
© 2004-2005 HIOX INDIA - hioxindia.com

Other Links