Hour 3. Using Functions

What You’ll Learn in This Hour

• How to define functions

• How to call (execute) functions

• How functions receive data

• Returning values from functions

• About the scope of variables

Commonly, programs carry out the same or similar tasks repeatedly during the course of their execution. For you to avoid rewriting the same piece of code over and over again, JavaScript has the means to parcel up parts of your code into reusable modules, called functions. After you’ve written a function, it is available for the rest of your program to use, as if it were itself a part of the JavaScript language.

Using functions also makes your code easier to debug and maintain. Suppose you’ve written an application to calculate shipping ...

Get Sams Teach Yourself JavaScript™ in 24 Hours, Fifth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.