Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 16 - WMLScript Functions Outline 16.1 Introduction 16.2 Program Modules in WMLScript 16.3 Programmer-Defined.

Similar presentations


Presentation on theme: " 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 16 - WMLScript Functions Outline 16.1 Introduction 16.2 Program Modules in WMLScript 16.3 Programmer-Defined."— Presentation transcript:

1  2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 16 - WMLScript Functions 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.6 Duration of Identifiers 16.7 Scope Rules

2  2001 Prentice Hall, Inc. All rights reserved. 2 16.2 Program Modules in WMLScript Fig. 16.1Hierarchical boss function/worker function relationship.

3  2001 Prentice Hall, Inc. All rights reserved. Outline 3 1 // Fig. 16.2: squareNumbers.wmls 2 // Programmer defined functions 3 4 extern function count() 5 { 6 // prompt for user input 7 var inputNumber = Dialogs.prompt( 8 "Enter a number to be squared", "" ); 9 10 // call function square and pass it inputNumber 11 var numberSquared = square( Lang.parseInt( inputNumber ) ); 12 13 // create output message 14 var outputSquare = inputNumber + " squared is " 15 + numberSquared; 16 17 WMLBrowser.setVar( "result1", outputSquare ); 18 WMLBrowser.go( "#result" ); 19 } 20 21 // variable y gets value of inputNumber 22 function square( y ) 23 { 24 return ( y * y ); 25 } SquareNumbers.wmls Call function square and pass it the value the user input as an integer. Function square takes the value pass to it in the function call. The number is multiplied by itself and the result is returned.

4  2001 Prentice Hall, Inc. All rights reserved. Outline 4 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Press OK to square a number. 18 19 20 21 22 23 24 25 26 27 28 29 $result1 30 31 32 fig16_3.wml The result of the number being squared is displayed by dereferencing browser variable result1.

5  2001 Prentice Hall, Inc. All rights reserved. Outline 5

6  2001 Prentice Hall, Inc. All rights reserved. Outline 6 1 // Fig 16.4: maximum.wmls 2 // Using the max function 3 4 extern function max() 5 { 6 // get values input in WML deck 7 var input1 = Lang.parseFloat( WMLBrowser.getVar( "number1" ) ); 8 var input2 = Lang.parseFloat( WMLBrowser.getVar( "number2" ) ); 9 var input3 = Lang.parseFloat( WMLBrowser.getVar( "number3" ) ); 10 11 // call function maximum and pass it input1, input2 and input3 12 var maxNumber = maximum( input1, input2, input3 ); 13 14 WMLBrowser.setVar( "maximumNumber", maxNumber ); 15 WMLBrowser.go( "#maximum" ); 16 } 17 18 // variables x, y and z get values 19 // of input1, input2 and input3 20 extern function maximum( x, y, z ) 21 { 22 return Lang.max( x, Lang.max( y, z ) ); 23 } maximum.wmls Method getVar retrieves the three numbers input by the user. Call function maximum and pass it the three number values. Function maximum takes the three float values passed to it. The max method is invoked twice to find the maximum of the three values input.

7  2001 Prentice Hall, Inc. All rights reserved. Outline 7 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Enter first number: 20 21 Enter second number: 22 23 Enter third number: 24 25 26 27 fig16_5.wml Three input boxes allow the user to input three numbers.

8  2001 Prentice Hall, Inc. All rights reserved. Outline 8 28 29 30 31 32 33 34 35 36 37 38 First number: $number1 39 Second number: $number2 40 Third number: $number3 41 Maximum is: $maximumNumber 42 43 44 fig16_5.wml The three numbers input and the largest of the three are displayed by dereferencing browser variables number1, number2, number3 and maximumNumber.

9  2001 Prentice Hall, Inc. All rights reserved. Outline 9

10  2001 Prentice Hall, Inc. All rights reserved. Outline 10 1 // Fig 16.6: guess.wmls 2 // Using the max function 3 4 extern function pickNumber() 5 { 6 // generate random number to be secret number 7 WMLBrowser.setVar( "secretNumber", ( 1 + 8 Lang.random( 100 ) ) ); 9 WMLBrowser.go( "#guess" ); 10 } // end function pickNumber 11 12 extern function newGuess() 13 { 14 // get value of secretNumber and convert to integer 15 var secret = Lang.parseInt( 16 WMLBrowser.getVar( "secretNumber" ) ); 17 18 // get value of nextGuess and convert to integer 19 var guess = Lang.parseInt( 20 WMLBrowser.getVar( "nextGuess" ) ); 21 22 // get value of tries and increment value by one 23 var guessCount = Lang.parseInt( 24 WMLBrowser.getVar( "tries" ) ) + 1; 25 26 WMLBrowser.setVar( "tries", guessCount ); 27 guess.wmls Generate a random number between 1 and 100. WMLBrowser method go sends the user to card guess. Update the number of tries.

11  2001 Prentice Hall, Inc. All rights reserved. Outline 11 28 // test if guess is correct 29 if ( guess secret ) { 34 WMLBrowser.setVar( "reply", "Too High" ); 35 WMLBrowser.go( "#check" ); 36 } 37 else if ( guess == secret ){ 38 WMLBrowser.go( "#winner" ); 39 } 40 } guess.wmls Test to see if the guess is low, high or correct.

12  2001 Prentice Hall, Inc. All rights reserved. Outline 12 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 The Guessing Game. 26 I'll think of a number from 1 to 100. 27 You try to guess it. 28 Press Start to begin. 29 30 31 fig16_7.wml Set the number of tries to 0 to start the game.

13  2001 Prentice Hall, Inc. All rights reserved. Outline 13 32 33 34 35 36 37 38 39 40 41 42 Enter your guess and press submit: 43 44 45 46 47 48 49 50 51 52 53 54 55 Your guess was $reply. Try again 56 57 58 fig16_7.wml Input field where the user enters a guess.Notify the user the guess was incorrect.

14  2001 Prentice Hall, Inc. All rights reserved. Outline 14 59 60 61 62 63 64 65 66 67 You win! My number was $secretNumber! 68 It only took you $tries guesses! 69 Press Restart and I will pick a new number. 70 71 72 fig16_7.wml Notify the user that the guess was correct.

15  2001 Prentice Hall, Inc. All rights reserved. Outline 15

16  2001 Prentice Hall, Inc. All rights reserved. Outline 16 1 // Fig 16.8: scoping.wmls 2 // Variable scoping in WMLScript 3 4 extern function start() 5 { 6 WMLBrowser.setVar( "x", "1" ); // browser variable 7 var x = 5; // variable local to function start 8 9 WMLBrowser.setVar( "display", "local x in start is " 10 + x + "\n" ); 11 12 functionA(); // has local x 13 functionB(); // uses browser variable x 14 functionA(); // reinitializes local x 15 functionB(); // browser variable x retains its value 16 17 WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) 18 + "\nlocal x in start is " + x ); 19 WMLBrowser.refresh(); 20 } // end function start 21 22 extern function functionA() 23 { 24 var x = 25; // initialized each time functionA is called 25 26 WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) 27 + "\nlocal x in functionA is " + x + 28 " after entering functionA" ); 29 30 ++x; // increment x 31 32 WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) 33 + "\nlocal x in functionA is " + x + 34 " before exiting functionA\n" ); 35 } // end functionA scoping.wmls To begin, browser variable x is assigned a value of 1. Display the value of x. Function calls. To begin, local variable x is assigned 5.The value of local x is changed to 25.Increment local variable x.

17  2001 Prentice Hall, Inc. All rights reserved. Outline 17 36 37 extern function functionB() 38 { 39 var changeX = WMLBrowser.getVar( "x" ); 40 41 WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) 42 + "\nbrowser variable x is " 43 + WMLBrowser.getVar( "x" ) + 44 " on entering functionB" ); 45 46 changeX *= 10; // multiply x by 10 47 48 WMLBrowser.setVar( "x", changeX ); 49 50 WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) 51 + "\nbrowser variable x is " 52 + WMLBrowser.getVar( "x" ) + 53 " on exiting functionB\n" ); 54 } // end functionB scoping.wmls Get the value of browser variable x.Multiply x by 10.

18  2001 Prentice Hall, Inc. All rights reserved. Outline 18 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 $display 18 19 20 fig16_9.wml Call function start. Display the results.

19  2001 Prentice Hall, Inc. All rights reserved. Outline 19


Download ppt " 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 16 - WMLScript Functions Outline 16.1 Introduction 16.2 Program Modules in WMLScript 16.3 Programmer-Defined."

Similar presentations


Ads by Google