• Aucun résultat trouvé

The “Secret” Code

Some JavaScript developers assign special meanings to return values to tell the statement that called the function what happened when the function processed the request. For example, the return value might indicate a specifi c error that occurred while the request was being processed. JavaScript developers call this an error code. Other times that return value indicates one of many outcomes of successfully processing the request, which is illustrated in the next JavaScript.

The next JavaScript is very similar to the previous JavaScript in that both defi ne two functions: one to handle the logon and the other to validate logon information.

However, the ValidateLogon() function in the following JavaScript uses a value of 1 to indicate that the logon information is valid and values of 2, 3, and 4 to indicate the portion of the logon information that is invalid.

ch05.indd 111

ch05.indd 111 4/26/2005 9:35:49 AM4/26/2005 9:35:49 AM

You saw this validation technique used in examples of if statements in Chapter 3

—except in those examples, the JavaScript displayed an appropriate message on the screen instead of returning a value.

<!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Return value from a function Statement</title>

<script language="Javascript" type="text/javascript">

ReturnValue = 2

<script language="Javascript" type="text/javascript">

In this chapter you learned how to divide your JavaScript applications into groups of statements, each of which performs one kind of task. These groups are called functions. You call a function whenever you need one of these tasks performed in your JavaScript.

You need to defi ne a function before calling it. A function defi nition consists of a function name, parentheses, and the function code block, which is where you place statements that are executed when the function is called.

A function can have all the information it needs to perform the task. Other func-tions need additional information passed to them from the statement that calls the functions. Information passed to a function is called an argument.

ch05.indd 113

ch05.indd 113 4/26/2005 9:35:50 AM4/26/2005 9:35:50 AM

An argument is placed between parentheses in the function defi nition and used as a variable within the function. More than one argument can be used; a comma must separate each argument.

A function can return a value to the statement that called the function by using a return statement. A return statement consists of the return keyword followed by the value that is being returned by the function. The statement that called the func-tion can assign the return value to a variable, use the return value in an expression, or ignore the return value.

You call a function by using the function name followed by parentheses. A func-tion can be called from anywhere in the JavaScript or by using HTML code in the web page.

Now that you have functions under your belt, it is time to move on. The next chapter discusses how to manipulate strings. Think of a string as any text and manipulating a string as a way for JavaScript to process the text.

Quiz

1. True or False. A comma must separate arguments in a function defi nition.

a. True b. False

2. A code block is used in a a. Function call

b. Function defi nition c. Return value d. Argument

3. The scope of a variable means a. The size of the variable b. The data type of the variable

c. The portion of a JavaScript that can access the variable d. The variable is used as a return value for a function

4. True or False. The statement that calls a function can ignore a value returned by a function.

a. True b. False

ch05.indd 114

ch05.indd 114 4/26/2005 9:35:50 AM4/26/2005 9:35:50 AM

5. A global variable can be accessed

a. Only by functions defi ned within the JavaScript b. Only outside of a function

c. Only by the function that defi ned it d. From anywhere in the JavaScript 6. A local variable can be accessed

a. Only by functions defi ned within the JavaScript b. Only outside of a function

c. Only by the function that defi ned it d. From anywhere in the JavaScript

7. True or False. A function can be called by HTML code in a web page.

a. True b. False

8. True or False. All functions must be defi ned in the <head> tag.

a. True b. False

9. True or False. Values passed to a function must correspond to the data type of arguments in the function defi nition.

a. True b. False

10. A variable is out of scope when

a. The statement that calls a function ignores the value returned by the function

b. The variable cannot be accessed by a statement c. A variable isn’t defi ned in a function

d. A variable is passed to a function

ch05.indd 115

ch05.indd 115 4/26/2005 9:35:50 AM4/26/2005 9:35:50 AM

ch05.indd 116

ch05.indd 116 4/26/2005 9:35:50 AM4/26/2005 9:35:50 AM

117

CHAPTER

Strings

When you order merchandise online, you probably give little thought to how your order is processed. Like most of us, you make a selection, enter credit card and shipping information, and then click a button on the order form. Several days later, a delivery van drops off your package. That’s all there is to it, right?

Actually, there’s a lot going on behind the scenes. Order information has to be extracted from the order form and then manipulated before being processed. You’ll learn how to extract information from a form in the next chapter. Most information you enter into an order form is a string, such as your name, address, phone number, and product information. You learned in Chapter 2 that a string is a series of char-acters that form text. It is often necessary to take apart and rearrange text so that the information can be processed properly. This is referred to as manipulating a string, and it’s a technique you’ll learn in this chapter.