Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [154]

By Root 1555 0

The program code breaks the parts of the song into the same pieces a song sheet does. Here are some interesting features of antsFunction.html:

♦ I created a function called chorus(). Functions are simply collections of code lines with a name.

♦ All the code for the chorus goes into this function. Anything I want as part of printing the chorus goes into the chorus() function. Later, when I want to print the chorus, I can just call the chorus() function and it will perform the code I stored there.

♦ Each verse has a function, too. I broke the code for each verse into its own function.

♦ The main code is a road map. When all the details are delegated to the functions, the main part of the code just controls the order in which the functions are called.

♦ Details are hidden in the functions. The main code handles the big picture. The details (how to print the chorus or verses) are hidden inside the functions.

Passing Data to and from Functions

Functions are logically separated from the main program. This separation is a good thing because it prevents certain kinds of errors. However, sometimes you want to send information to a function. You may also want a function to return some type of value. The antsParam.html page rewrites the “The Ants Go Marching” song in a way that takes advantage of function input and output.

I don’t provide a figure of this program because it looks just like antsFunction.html to the user. One advantage of functions is that I can improve the underlying behavior of a program without imposing a change in the user’s experience.

This code incorporates a couple important new ideas. (The following list is just the overview; the specifics are coming in the following sections.)

♦ These functions return a value. The functions no longer do their own alerts. Instead, they create a value and return it to the main program.

♦ Only one verse function exists. Because the verses are all pretty similar, using only one verse function makes sense. This improved function needs to know what verse it’s working on to handle the differences.

Examining the main code

The main code has been changed in one significant way. In the last program, the main code called the functions, which did all the work. This time, the functions don’t actually output anything themselves. Instead, they collect information and pass it back to the main program. Inside the main code, each function is treated like a variable.

You’ve seen this behavior. The prompt() method returns a value. Now the chorus() and verse() methods return values. You can do anything you want to this value, including storing it to a variable, printing it, or comparing it to some other value.

Separating the creation of data from its use as I’ve done here is a good idea. That way, you have more flexibility. After a function creates some information, you can print it to the screen, store it on a Web page, put it in a database, or whatever.


Looking at the chorus

Return Main Page Previous Page Next Page

®Online Book Reader