02.06.2013 Views

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

var formattedTotal = total.toFixed(2);<br />

return formattedTotal;<br />

}<br />

<strong>The</strong> first line stores the tax rate into a variable named TAX (which lets you easily<br />

change the rate simply by updating this line of code). <strong>The</strong> next three lines define the<br />

function. Don’t worry too much about what’s happening inside the function—you’ll<br />

learn more about working with numbers on page 445. <strong>The</strong> important part is the<br />

fourth line of the function—the return statement. It returns the value stored in the<br />

variable formattedTotal.<br />

To make use of the return value, you usually store it inside a variable. So in this<br />

example, you could call the function like this:<br />

var saleTotal = calculateTotal(2, 16.95);<br />

document.write('Total cost is: $' + saleTotal);<br />

In this case, the values 2 and 16.95 are passed to the function. <strong>The</strong> first number<br />

represents the number of items purchased, and the second their individual cost. <strong>The</strong><br />

function determines the total cost plus tax and returns the total: That result is then<br />

stored into a new variable—saleTotal—which is then used as part of a document<br />

.write() command to print the total cost of the sale including tax.<br />

Note: <strong>The</strong> return keyword should be the last statement in a function, since as soon as a browser’s<br />

<strong>JavaScript</strong> interpreter encounters the return statement, it exits the function. Any lines of code following the<br />

return statement in the function are never executed.<br />

You don’t have to store the return value into a variable, however. You can use the<br />

return value directly within another statement like this:<br />

document.write('Total: $' + calculateTotal(2, 16.95));<br />

In this case, the function is called and its return value is added to the string ‘Total:<br />

$’, which is then printed to the document. At first, this way of using a function may<br />

be hard to read, so you might want to take the extra step of just storing the function’s<br />

results into a variable and then using that variable in your script.<br />

Note: A function can only return one value. If you want to return multiple items, store the results in an<br />

array, and return the array.<br />

Keeping Variables from Colliding<br />

One great advantage of functions is that they can cut down the amount of programming<br />

you have to do. You’ll probably find yourself using a really useful function time<br />

and time again on different projects. For example, a function that helps calculate<br />

shipping and sales tax could come in handy on every order form you create, so you<br />

might copy and paste that function into other scripts on your site or on other projects.<br />

chapter 3: adding logic and control to your programs<br />

www.it-ebooks.info<br />

Functions: Turn<br />

Useful Code Into<br />

Reusable Commands<br />

105

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!