Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

WMLScript Functions: Declaration, Libraries, and Recursion, Slides of Fundamentals of E-Commerce

An introduction to wmlscript functions, including how to declare them, use libraries, and implement recursion. It covers topics such as variable types, initialization, conditions, and common language statements.

Typology: Slides

2012/2013

Uploaded on 07/29/2013

sharad_984
sharad_984 🇮🇳

4.5

(13)

146 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
WMLScript - Functions
Within each function, you’ll write WMLScript
code.
Syntax:
var n = 1; // Declares a variable named ‘n’
n = 2; // Assigns n the value 2
if (n==3) ... // Test if n equals 3.
// Note that ‘=‘, assignment, is not the
// same as ‘==‘, which tests equality.
You’ve got the full set of if-then tests, loops and so on...
for (var index = 0; index < 100; index++) {
myFunc(index);
};
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download WMLScript Functions: Declaration, Libraries, and Recursion and more Slides Fundamentals of E-Commerce in PDF only on Docsity!

WMLScript - Functions

Within each function, you’ll write WMLScriptcode.

Syntax:

var n = 1;

// Declares a variable named ‘n’

n

// Assigns n the value 2

if (n==3) ...

// Test if n equals 3.// Note that ‘=‘, assignment, is not the// same as ‘==‘, which tests equality.

You’ve got the full set of

if-then

tests, loops and so on...

for

(var

index

index

index++)

myFunc(index);

WMLScript - Functions

Declaring a function of your own:

Ex:

extern function getHello(){

return “hello!”;

Ex:

extern function addOne(n){

return n+1;

Ex:

extern function addVals(n, m){

return n+m;

WMLScript - Functions

The WMLScript Libraries:

Lang (miscellaneous routines)

Float (real numbers)

String (character string manipulation)

URL (HTTP string routines)

WMLBrowser (browser interraction)

Dialogs (user interraction)

Console (debug output)

Each library defines a set of functions that can becalled from your code.

For details on each library, consult the referencematerials online.

WMLScript - Functions

A few common standard library routines:

^

WMLBrowser.getVar()

// gets a var’s value

^

WMLBrowser.setVar(, )

// sets a var’s value

^

WMLBrowser.refresh()

// reload this page

^

WMLBrowser.go(url)

// go to a url

^

Lang.isInt()

// Test for a number

^

Lang.parseInt()

// Convert to a number

Some common language statements:

^

if () else

^

for (; ; ) ...

^

while () ...

^

return ;

// Sets the value of// a function call

WMLScript - Calling WMLS from WML

extern function increment() {

var myVar = WMLBrowser.getVar("myVar");WMLBrowser.setVar("myVar",

myVar + 1

WMLBrowser.refresh();

What’s going

on here?

WMLScript - What happened back there?

Variable types

The problem was that variables in WMLScript don’t have a type

associated with them. WMLS doesn’t know whether

they’re character strings or numbers.

Since “+” will add two numbers

or

glue together two strings

(1+1=2, but ‘abc’+’def’=‘abcdef’), WMLS just assumed wewanted string concatenation.

To make sure we use integer math, use the

Lang

library’s

toInt()

function to force the variable type to an integer:

extern function increment() {

var myVar = WMLBrowser.getVar("myVar"); if (!Lang.isInt(myVar))

myVar = 0;

WMLBrowser.setVar("myVar",

Lang.parseInt(myVar) + 1

WMLBrowser.refresh();

WMLScript - Initialisation and conditions

Testing a value in WMLS:

extern

function

testValue() {

var

myVar =

Lang.parseInt(WMLBrowser.getVar("myVar"));

if

(((myVar

/^

myVar)

WMLBrowser.setVar("myOddity", "even"); else

WMLBrowser.setVar("myOddity", "odd"); WMLBrowser.refresh();

And the result:

WMLScript - Recursion

“Recursion”

Recursion is the term for when a function calls itself. Whenyou write recursive code, take care : make sure that there’salways a way out!

Ex:

In mathematics, the phrase “N factorial” means “if N is some

number, then multiply all of the numbers from 1 to Ntogether”. You could say that a little more easily as “if Nis some number, then multiply N times N minus one,factorial”. You’ve defined factorial in terms of itself.

The shorthand for factorial is !. “N!” is “N factorial”.In other words, N!= N * (N-1)!.

WMLScript - Recursion

<card

id="card1"> <onevent

type="onenterforward">

<setvar

name="nFact"

value="<untested>"

Enter N

:^

<input

name="n"

format="N*N"/>

Click here

to

find N!

("N

factorial").


N!

$(nFact).

extern

function

factorial()

var

n^

Lang.parseInt(WMLBrowser.getVar("n")); if

(n

n^

=^

WMLBrowser.setVar("nFact",

recurseFactorial(n)

WMLBrowser.refresh(); } extern

function

recurseFactorial(n)

if

(n

return

n

recurseFactorial(n-

else

return

n;