PDA

View Full Version : Understanding the "return" statement


tiltmode
04-20-2003, 11:29 PM
I am learning the basics of JavaScript. I am having trouble understanding when to use the "return" statement in my scripts. The book I am learning from is called Using Javascript by Paul Mcfedries (mcfedries.com (http://www.mcfedries.com/UsingJavascript)). The script that I first noticed the "return" statement was in this script:

<script language="JavaScript" type="text/javascript">
<!--

var pre_tip_total = 100.00
var tip_cost
var total_bill

function calculate_tip(pre_tip) {
var tip_result
tip_result = pre_tip * 0.15
return (tip_result)
}

tip_cost = calculate_tip(pre_tip_total)
total_bill = pre_tip_total + tip_cost
alert("Your total bill is $" + total_bill)

//-->
</script>

I would greatly appreciate if someone could explain to me when the "return" statement is necessary, or direct me to a tutorial that explains this.

thanks a million.

HairyTeeth
04-21-2003, 12:28 AM
Hi,
A function has to return a value to whatever calls it (or whenever it is called). In effect, 'return' says "return this value". Take a look at the functions below:

function foo(){
var a = 1
var b = 2
var total = a + b
}

function callFoo(){
alert(foo())
}

In the foo() function, we've done all our calculations to get our total. But when we call the foo() function with the callFoo() function, it returns 'undefined'. Thats because foo() is not returning a value.

However, if we put in the return statement like so:

function foo(){
var a = 1
var b = 2
var total = a + b
return total
}

function callFoo(){
alert(foo())
}


...the function will return a value (3).

You don't need to use the return statement if a value is not being sent somewhere. For example:

function foo(){
var a = 1
var b = 2
var total = a + b
alert(total)
}


In your script, a value is being passed as an argument (parameter) called 'pre_tip". The value for pre_tip might come from a form textbox (whatever).

The function calculate_tip(pre_tip) is passed the value of pre_tip (from a textbox for example), multiples it by 0.15 and puts that result into a variable called tip_result .

Whenever the calcualte_tip() function is called, it will return the value of tip_result to whatever called it. In this case, a variable called tip_cost holds the value of tip_result. Some more math is done and you end up with a bill. :)

Hope that helped.

Spookster
04-21-2003, 12:39 AM
The "return" keyword is pretty common among most scripting and programming languages. It is used to return a value from a method or function. Here is an example I put together:



<html>
<head>

<script language="Javascript" type="text/javascript">
<!--//

function swapLetter(letter){
if(letter == 'A')
letter = 'B';
else if(letter == 'B')
letter = 'A';
else
letter = 'C';

return letter;
}

function showLetter(letter){
newLetter = swapLetter(letter);
alert(newLetter);
}




//-->
</script>

</head>
<body>
<br>
<br>
<br>

<a href="javascript:showLetter('A');">Swap letter A</a>
<br>
<br>
<a href="javascript:showLetter('B');">Swap letter B</a>
<br>
<br>
<a href="javascript:showLetter('H');">Swap letter H</a>

</body>
</html>

Graeme Hackston
04-21-2003, 01:40 AM
another handy use


a = true

function one() {
if (two()) {
// do something
} else {
// do else
}
}

function two() {
if (a) {
return true
} else {
return false
}
}