Chapter 7

Getting Functional

IN THIS CHAPTER

Bullet Writing functions

Bullet Documenting functions

Bullet Passing parameters

Bullet Returning values

Bullet Organizing programs with functions

“I write as a function. Without it I would fall ill and die. It’s much a part of one as the liver or intestine, and just about as glamorous.”

— CHARLES BUKOWSKI

Functions help you reduce code repetition by turning frequently used bits of code into reusable parts. In this chapter, you write some functions and use them to make otherwise tedious tasks easy and fun!

Understanding the Function of Functions

Functions are mini programs within your programs. Functions serve to handle tasks within the main program that may be required multiple times by different parts of the program.

If you’ve read any of the preceding chapters, you’ve seen a few functions in action. The following example is a simple function that, when run, simply adds a z to the end of a string.

function addZ(aString) { aString += "z"; return aString;}

To try this ...

Get Coding All-in-One For Dummies, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.