Program Modules in WMLScript
Program Modules in WMLScript Modules in WMLScript are called functions. WMLScript programs are written by combining new functions that the programmer writes with “prepackaged” functions and objects available in WMLScript. The “prepackaged” functions that belong to WMLScript objects (such as Lang.parseInt and WMLBrowser.go, introduced previously) are called methods. The term method implies that the function belongs to a particular object; however, the terms function and method can be used interchangeably. We will refer to functions that belong to a particular WMLScript object as methods; all others are referred to as functions. WMLScript provides several objects that have a rich collection of methods for performing common mathematical calculations, string manipulations, browser manipulations and other functions. These provide many of the capabilities programmers need. Some common predefined objects of WMLScript and their methods are discussed in Chapter 19, WMLScript: Objects. Good Programming Practice 16.1 Familiarize yourself with the rich collection of objects and methods provided by WMLScript. 16.1 Software Engineering Observation 16.1 Avoid reinventing the wheel. If possible, use WMLScript objects, methods and functions instead of writing new functions. This reduces program development time and avoids introducing new errors. 16.1 Outline 16.1 Introduction 16.2 Program Modules in WMLScript 16.3 Programmer-Defined Functions 16.4 Function Definitions 16.5 Random Number Generation 16.8 Example: A Game of Chance 16.6 Duration of Identifiers 16.7 Scope Rules Summary • Terminology • Self-Review Exercises • Answers to Self-Review Exercises • Exercises Chapter 16 WMLScript: Functions 283 © Copyright 2001 by Deitel & Associates, Inc. All Rights Reserved. review Packet 5/7/01. Portability Tip 16.1 Using the methods built into WMLScript objects helps make programs more portable. 16.1 Performance Tip 16.1 Do not try to rewrite existing WMLScript methods to make them more efficient. You usually will not be able to increase the performance of these methods. 16.1 In addition to using existing functionality, a programmer can write functions to define specific tasks that may be used at various points in a script. These are sometimes referred to as programmer-defined functions. A function is invoked (i.e., made to perform its designated task) by a function call. The function call specifies the function name and provides information (as arguments) that the called function needs to do its task. A common analogy for this structure is the hierarchical form of management. A boss (the calling function or caller) asks a worker (the called function) to perform a task and return (report back) the results when the task is done. The boss function does not know how the worker function performs its designated tasks. The worker may call other worker functions, and the boss will be unaware of this. We will soon see how this “hiding” of implementation details promotes good software engineering. Figure 16.1 shows the boss function communicating with several worker functions in a hierarchical manner. Note that worker1 acts as a “boss” function to worker4 and worker5. Relationships among functions may be other than the hierarchical structure shown in this figure. . Fig. 16.1 Hierarchical boss function/worker function relationship. Functions (and methods) are called (invoked) by writing the name of the function (or method), followed by a left parenthesis, followed by the argument (or a comma-separated list of arguments) followed by a right parenthesis. For example, a programmer desiring to convert a string stored in variable inputValue to an integer number and add it to variable total, might write total += Lang.parseInt( inputValue ); boss worker1 worker2 worker3 worker4 worker5 284 WMLScript: Functions Chapter 16 © Copyright 2001 by Deitel & Associates, Inc. All Rights Reserved. review Packet 5/7/01. When this statement executes, WMLScript’s Lang object method parseInt converts the string contained in the parentheses (stored in variable inputValue in this case) to an integer value and that value is added to total. The variable inputValue is the argument of the parseInt method. Method parseInt takes a string representation of an integer as an argument and returns the corresponding integer value. Function (and method) arguments may be constants, variables or expressions. If s1 = "22.3" and s2 = "45", then the statement total += Lang.parseInt( s1 + s2 ); evaluates the expression s1 + s2, concatenates the strings s1 and s2 (resulting in the string "22.345"), converts the result into an integer and adds the floating-point number to variable total.
702 times read
|
|
|
|