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
2001 Prentice Hall, Inc. All rights reserved Program Modules in WMLScript Fig. 16.1Hierarchical boss function/worker function relationship.
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 ) ); // create output message 14 var outputSquare = inputNumber + " squared is " 15 + numberSquared; WMLBrowser.setVar( "result1", outputSquare ); 18 WMLBrowser.go( "#result" ); 19 } // 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.
2001 Prentice Hall, Inc. All rights reserved. Outline Press OK to square a number $result fig16_3.wml The result of the number being squared is displayed by dereferencing browser variable result1.
2001 Prentice Hall, Inc. All rights reserved. Outline 5
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" ) ); // call function maximum and pass it input1, input2 and input3 12 var maxNumber = maximum( input1, input2, input3 ); WMLBrowser.setVar( "maximumNumber", maxNumber ); 15 WMLBrowser.go( "#maximum" ); 16 } // 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.
2001 Prentice Hall, Inc. All rights reserved. Outline Enter first number: Enter second number: Enter third number: fig16_5.wml Three input boxes allow the user to input three numbers.
2001 Prentice Hall, Inc. All rights reserved. Outline First number: $number1 39 Second number: $number2 40 Third number: $number3 41 Maximum is: $maximumNumber fig16_5.wml The three numbers input and the largest of the three are displayed by dereferencing browser variables number1, number2, number3 and maximumNumber.
2001 Prentice Hall, Inc. All rights reserved. Outline 9
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", ( Lang.random( 100 ) ) ); 9 WMLBrowser.go( "#guess" ); 10 } // end function pickNumber extern function newGuess() 13 { 14 // get value of secretNumber and convert to integer 15 var secret = Lang.parseInt( 16 WMLBrowser.getVar( "secretNumber" ) ); // get value of nextGuess and convert to integer 19 var guess = Lang.parseInt( 20 WMLBrowser.getVar( "nextGuess" ) ); // get value of tries and increment value by one 23 var guessCount = Lang.parseInt( 24 WMLBrowser.getVar( "tries" ) ) + 1; 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.
2001 Prentice Hall, Inc. All rights reserved. Outline // 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.
2001 Prentice Hall, Inc. All rights reserved. Outline The Guessing Game. 26 I'll think of a number from 1 to You try to guess it. 28 Press Start to begin fig16_7.wml Set the number of tries to 0 to start the game.
2001 Prentice Hall, Inc. All rights reserved. Outline Enter your guess and press submit: Your guess was $reply. Try again fig16_7.wml Input field where the user enters a guess.Notify the user the guess was incorrect.
2001 Prentice Hall, Inc. All rights reserved. Outline You win! My number was $secretNumber! 68 It only took you $tries guesses! 69 Press Restart and I will pick a new number fig16_7.wml Notify the user that the guess was correct.
2001 Prentice Hall, Inc. All rights reserved. Outline 15
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" ); functionA(); // has local x 13 functionB(); // uses browser variable x 14 functionA(); // reinitializes local x 15 functionB(); // browser variable x retains its value WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) 18 + "\nlocal x in start is " + x ); 19 WMLBrowser.refresh(); 20 } // end function start extern function functionA() 23 { 24 var x = 25; // initialized each time functionA is called WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) 27 + "\nlocal x in functionA is " + x + 28 " after entering functionA" ); x; // increment x 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.
2001 Prentice Hall, Inc. All rights reserved. Outline extern function functionB() 38 { 39 var changeX = WMLBrowser.getVar( "x" ); WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) 42 + "\nbrowser variable x is " 43 + WMLBrowser.getVar( "x" ) + 44 " on entering functionB" ); changeX *= 10; // multiply x by WMLBrowser.setVar( "x", changeX ); 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.
2001 Prentice Hall, Inc. All rights reserved. Outline $display fig16_9.wml Call function start. Display the results.
2001 Prentice Hall, Inc. All rights reserved. Outline 19