Online Book Reader

Home Category

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

By Root 1564 0

The chorus of “The Ants Go Marching” song program has been changed to return a value. Take another look at the chorus() function to see what I mean.

function chorus() {

var text = “...and they all go marching down\n”;

text += “to the ground \n”;

text += “to get out \n”;

text += “of the rain. \n”;

text += “ \n”;

text += “boom boom boom boom boom boom boom boom \n”;

return text;

} // end chorus

Here’s what changed:

♦ The purpose of the function has changed. The function is no longer designed to output some value to the screen. Instead, it now provides text to the main program, which can do whatever it wants with the results.

♦ There’s a variable called text. This variable contains all the text to be sent to the main program. (It contained all the text in the last program, but it’s even more important now.)

♦ The text variable is concatenated over several lines. I used string concatenation to build a complex value. Note the use of new lines (\n) to force carriage returns.

♦ The return statement sends text to the main program. When you want a function to return some value, simply use return followed by a value or variable. Note that return should be the last line of the function.

Handling the verses

The verse() function is quite interesting:

♦ It can print more than one verse.

♦ It takes input to determine which verse to print.

♦ It modifies the verse based on the input.

♦ It returns a value, just like chorus().

To make the verse so versatile (get it? verse-atile!), it must take input from the primary program and return output.


Passing data to the verse() function

The verse() function is always called with a value inside the parentheses. For example, the main program sets verse(1) to call the first verse, and verse(2) to invoke the second. The value inside the parentheses is called an argument.

The verse function must be designed to accept an argument (because I call it using values inside the parentheses). Look at the first line to see how.

function verse(verseNum){

In the function definition, I include a variable name. Inside the function, this variable is known as a parameter. (Don’t get hung up on the terminology. People often use the terms parameter and argument interchangeably.) The important idea is that whenever the verse() function is called, it automatically has a variable called verseNum. Whatever argument you send to the verse() function from the main program will become the value of the variable verseNum inside the function.

You can define a function with as many parameters as you want. Each parameter gives you the opportunity to send a piece of information to the function.


Determining the distraction

If you know the verse number, you can determine what distracts “the little one” in the song. You can determine the distraction in a couple ways, but a simple if-else if structure is sufficient for this example.

var distraction = “”;

if (verseNum == 1){

distraction = “suck his thumb.”;

} else if (verseNum == 2){

distraction = “tie his shoe.”;

} else {

distraction = “I have no idea.”;

}

I initialized the variable distraction to be empty. If verseNum is 1, set distraction to “suck his thumb.” If verseNum is 2, distraction should be “tie his shoe”. Any other value for verseNum is treated as an error by the else clause.

If you’re an experienced coder, you may be yelling at this code. It still isn’t optimal. Fortunately, in the section “Building a basic array” later in this chapter, I show an even better solution for handling this particular situation with arrays.

By the time this code segment is complete, verseNum and distraction both contain a legitimate value.


Creating the text

When you know these variables, it’s pretty easy to construct the output text:

var text = “The ants go marching “;

text += verseNum + “ by “ + verseNum + “ hurrah, hurrah \n”;

text += “The ants go marching “;

text += verseNum + “ by “ + verseNum + “ hurrah, hurrah \n”;

text += “The ants go marching “;

text += verseNum

Return Main Page Previous Page Next Page

®Online Book Reader