Chapter 4. Functions and Arrays
In This Chapter
Getting organized with functions
Passing parameters into functions
Returning values from functions
Functions and variable scope
Producing basic arrays
Retrieving data from arrays
Building a multidimensional array
It doesn't take long for your code to become complex. Soon enough, you'll find yourself wanting to write more sophisticated programs. When things get larger, you need new kinds of organizational structures to handle the added complexity.
You can bundle several lines of code into one container and give this new chunk of code a name: function. You can also take a whole bunch of variables, put them into a container, and give it a name. That's called an array.
This chapter is about how to work with more code and more data without going crazy.
Breaking Code into Functions
Functions come in handy when you're making complex code easier to handle.
Functions are a useful tool for controlling complexity. You can take a large complicated program and break it into several smaller pieces. Each piece stands alone and solves a specific part of the overall problem.
You can think of each function as a miniature program. You can define variables in functions, put loops and branches in there, and do anything else you can do with a program. A program using functions is basically a program full of subprograms.
Note
Once you have functions defined, they're just like new JavaScript commands. In a sense, when you add functions, you're adding to JavaScript.
To explain ...
Get HTML, XHTML, and CSS All-In-One Desk Reference For Dummies® 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.