Exforsys.com
 
Home Tutorials JavaScript
 

Passing values to JavaScript Function

 

Returning Values from Function

Page 2 of 2


Returning Values from Function:

After executing the function, there may be situations where the programmer wants to return some value from the function to the calling program. This is achieved by using the return statement in JavaScript.



The general syntax of this return statement is as follows:



return variablename



An example to understand this concept:


Suppose the programmer wants to create a function with name exforsys, which takes two values x and y. The function is written to add these values and return the result of the addition to the calling program. This is achieved in JavaScript as follows:



function exforsys(x,y)
{
z=x+y
return z
}



Thus, in the above example, the added value x+y is stored in variable z and returned to the calling program using the statement



return z



In the calling program, suppose the values passed to the function from the calling program are 10 and 15. From the calling program, the function is called as follows:



result=exforsys(10,15)



The returned value z is the addition of two values x and y of the function exforsys . This is stored in the variable result in the calling program.


A complete example of a function in JavaScript:



<
html>
<
head>
<
script type="text/javascript">
function exforsys(x,y)
{
return x+y
}
script>
head>


<body>
<script type="text/javascript">
document.write(exforsys(10,15))
script>
body>
html>




The output of the above program is



25



Thus, in the above example, the function creation is done on the <head> section of the program. The function exforsys is called with values 10 and 15 passed to the function and the added value 25 is returned from the function using the return statement. This is stored in the function name exforsys and then printed in the calling program.




First Page: Passing values to JavaScript Function


Read Next: JavaScript Two Dimensional Arrays



 

 

Comments


rasla said:

  The data is insufficient to find an answer
January 13, 2009, 4:08 am

vijaya said:

  how to pass dynamic values to java script function
e.g. <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClientClick="disp_alert()"/>
in "disp_alert()" function i want to pass the value entered in textbox having id "txtName".
January 21, 2009, 2:49 am

john anderson said:

  Can you give an example of passing values from one class to the next?
March 22, 2009, 9:47 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape