Jump to content

Return Statement


gaurav bhardwaj

Recommended Posts

if i take this example

 

 

function add_num(){

 

var num1=10;

var num2=10;

var num3=num1+num2;

document.write(num3);

 

}

add_num();

 

 

and

 

function add_num(){

 

var num1=10;

var num2=10;

var num3=num1+num2;

return num3;

 

}

var total_num=add_num();

document.write(total_num);

 

Q.1-:Why do we use return statement ,

Q.2-:When we get the same answer by example no .1 so why do we use return statement.

Q.3-:If I change the return statement like ;(return 50;) and call the function ,I got the 50 .rather then it should be 20. why.

Link to comment
Share on other sites

You use a return statement when you want or need a return value. Back in the olden days there were functions and subroutines -- functions returned a value but subroutines did not. In Javascript a function isn't required to return a value. For example...

function addnums(x,y,z){var s = x+y+z;return s; }var summit = addnums(155,66.777,2000);
function clearvalues(){var list = document.getElementsByTagName('input');for (var i=0,len=list.length ; i<len ; i++){  if (list[i].type=='text'){list[i].value = '';}}}
Link to comment
Share on other sites

return statements are used to get values from functions. In an ideal world a function only does one thing and does it well; data goes in, data comes out. Taking your example, I wouldn't write a function to do both the addition and the rendering. I would simply have the function calculate over my inputs and return the value of that calculation. I can then choose to do what I please with that value which could be displaying it, or using it another function. This makes the function decoupled and modular.

Link to comment
Share on other sites

Thank you for reply,But i do not understand the concept.Yes this is right return statement use to get a value return from function ,But Where should I use that ,because i am getting same answer by example first,And if i fix the value in return statement so I will get the that value rather It does matter how the function design or write

Edited by gaurav bhardwaj
Link to comment
Share on other sites

function add_num(num1,num2){var num3= Number(num1)+Number(num2);return num3;}var total_num = add_num(10,10);

...or...

function add_num(num1,num2){return Number(num1)+Number(num2);}var total_num = add_num(10,10);

(Note that I added the Number() function to prevent a common Javascript problem which occurs when textbox strings are used as numbers.)

Link to comment
Share on other sites

Thank you for reply,But i do not understand the concept.Yes this is right return statement use to get a value return from function ,But Where should I use that ,because i am getting same answer by example first,And if i fix the value in return statement so I will get the that value rather It does matter how the function design or write

That's exactly the point I was trying to make. Don't write the value out in your function, if it's already doing something else. The function should just add in this case. Again, this all about best practices and experience. You may not see or understand the value now, but in more complex programs, it will matter, when seemingly one small change has a rippling effect because things are too coupled.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...