Chapter 4. Functions, Arrays, and Objects
In This Chapter
Passing parameters into functions
Returning values from functions
Functions and variable scope
Producing basic arrays
Retrieving data from arrays
Building a multidimensional array
Creating objects
Building object constructors
Introducing JSON notation
It doesn't take long for your code to become complex. Soon enough, you 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: that is a 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. If you combine functions and data, you get another interesting structure called an object.
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 – 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
After you define ...
Get HTML, XHTML, & CSS 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.