Learn Javascript
Value of a Function
Join the Discussion
More of this Tutorial
Related Terms
Introduction.
In the last tutorial you learned how to pass the vaues of variables to a function using parameters. In this one you will learn how the function can also pass one value back to the calling code. This single value that is returned is considered to be the value of the function itself.
Returning a Value
A function can return a single value back to the code that called it using the return statement. You can have as many return statements as you like within a function but only one of them will run for each time that the function is called. It is not necessary to define a variable in order to return it from a function, you can pass static values back instead or even the result of a calculation. Here's an example:
if (fld == '') return false;
return true;
}
This function will return false if the parameter passed to it is empty text and will return true if the parameter passed contains anything. In this example we have used two return statements to return whichever of the two values applies but if the code were actually this simple we could also do it with just one return statement like this:
return (fld != '');
}
Here the value of the argument is compared to empty text and the result of the not equal comparison is returned. Basically you can include whatever you want into a return statement and the resultant value of whatever calculation is to be done will be returned to the calling code.
Processing a Returned Value
So now we know how to return a value we now need to know how to use that returned value in the calling code. Well the way that you use this value is to treat the actual function statement itself as if it were a variable. The value of the function itself is the value that was returned. Here's an example:
if (!validField(myField)) {
document.write('not ');
}
document.write('empty');
The function call itself can be used anywhere that a variable can be used as long as you match the type of the value being returned to the type of variable that would be used in that code. If you are performing a mathematical calculation then the value returned from a function that is called from that calculation should be numeric.
Using What You Know
Let's take another look at the example code from the tutorial about the if statement. We are still offering a discount only this time instead of our specifying the discounted price we are going to call a function to calculate it. Here is the code from that tutorial modified to call a function to calculate the discount amount:
var regPrice = 25;
var discount = discAmount(regPrice,discPercent);
var salePrice = regPrice - discount;
if (salePrice > 0)
document.write('<p>Save $'+discount+
' off the normal price of $'
+regPrice+ 'now only $'+salePrice
+'.</p>');
else
document.write('<p>Buy now at our regular cheap price of $'
+regPrice+'.</p>');
Of course we also need the function that calculates the SalePrice which looks like this:
var SalePrice = price * disc / 100;
return SalePrice;
}
Notice that I have actually defined a variable this time just to show you that you can define a variable to use to return a value if you want to instead of just returning the result of the calculation directly. You will also note that I have given it the same name as another variable defined outside the function. As you will remember from the tutorial on variable scope the salePrice variable within the function is separate from the one outside the function and the one outside will not be accessible while the function is processing. The return statement assigns the value of this local variable to the function itself when we return from processing the function so that it becomes available to the calling code.

