Chapter 7
Getting Functional
IN THIS CHAPTER
Writing functions
Documenting functions
Passing parameters
Returning values
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 to the end of a string. z
function addZ(aString) { aString += "z"; return aString;}
To try this ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access