• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 4. Working with Functions > Returning a Value from a Function

Returning a Value from a Function

So far you've seen two major advantages of using functions: You can use them to control when code is executed, and you can use them to consolidate repetitive code into a single routine. The third major benefit that functions bring to the JavaScript table is that you can use them to perform calculations and then return the result.

As an example, let's construct a simple function that calculates the appropriate tip on a restaurant bill. Assuming "appropriate" means 15%, the script in Listing 4.5 does the job.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint