 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.

Slides:



Advertisements
Similar presentations
JavaScript: Functions
Advertisements

 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
1 JavaScript: Control Structures II. 2 whileCounter.html 1 2
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Structured programming
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Outline 4.1 Introduction 4.2 Program Components in Python 4.3 Functions 4.4Module.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 JavaScript/Jscript 4 Functions. 2 Introduction Programs that solve real-world programs –More complex than programs from previous chapters Best way to.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
Fundamentals of Python: From First Programs Through Data Structures
1 JavaScript: Functions and Arrays October 18, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Fundamentals of Python: First Programs
WML II (“Son of WML”) WML WMLScript. WML - A Quick Review Document structure ,... Text and image controls ...,,..., Navigation controls ,,, Events.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
CS1101X: Programming Methodology Recitation 3 Control Structures.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 18 - WMLScript: Control Structures II Outline 18.1 Introduction 18.2 Essentials of Counter-Controlled.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 23 - Wireless Internet and m- Business Outline 23.1 Introduction 23.2 M-Business 23.3 Identifying.
Discussion 4. Labs public MyPoint(double xInit, double yInit ) { MyPoint p = new MyPoint(0, 0); } ClassProblem.java recursive java.lang.StackOverflowError.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 16.1Introduction 16.2Program Modules in JavaScript 16.3Programmer-Defined Functions 16.4Function.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 5: Functions Xiang Lian The University of Texas – Pan American Edinburg, TX
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
UFCEKS-20-2Multimedia Authoring Times Table Quiz.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing a Line of Text in a Web Page 7.3 Another JavaScript.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
REEM ALMOTIRI Information Technology Department Majmaah University.
Functions and an Introduction to Recursion
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 10 - JavaScript: Functions
Chapter 5 - Functions Outline 5.1 Introduction
JavaScript: Functions
Chapter 6 - Functions Outline 5.1 Introduction
Chapter 23 - Wireless Internet and m-Business
Chapter 10 - JavaScript: Functions
Presentation transcript:

 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