Defining WMLScript Functions

Preferences - Do not show ads

In WMLScript, all code must be encapsulated in functions. This is different from JavaScript in which a web developer can choose whether to place the code in functions or directly in the markup. A function in WMLScript is defined using the following format. The parts enclosed inside brackets [] are optional.

[extern] function function_name([argument1, argument2...])
  WMLScript statements here
  [return (some_value);]

The extern Keyword

The extern keyword is used to specify that a function can be called from both inside and outside of the WMLScript file, i.e. the function can be called from functions in the same WMLScript file, from functions in a different WMLScript file, or in a WML file. If you define a function without the extern keyword, the function can only be called from functions in the same WMLScript file.

WMLScript Function Naming Conventions

In WMLScript, function names must be started with a letter or an underscore character. The rest of the characters can be letters, numbers, or underscores. Special characters other than the underscore are not permitted. You cannot use WMLScript reserved words and keywords as function names. Remember that WMLScript is case-sensitive. The names wmlscript_function and WMLScript_Function refer to two different functions.

Here are some examples of valid function names:

  • wmlscriptFunction

  • _wmlscript_function

  • wmlscript_function1

  • wmlscript_1st_function


  • ____________

And here are some examples of invalid function names:

  • 1st_wmlscript_function (Reason: you cannot start a function name with a number)

  • 11111 (Reason: You cannot start a function name with a number)

  • wmlscript-function (Reason: The - character is not permitted)

  • ~wmlscript_function (Reason: The ~ character is not permitted)

WMLScript Function Arguments

Arguments are used to pass values into a function. Unlike programming languages like C++ or Java, WMLScript does not require you to specify the data type of an argument. For example, to pass two numbers into the WMLScript function wmlscript_function(), you will define something like this:

function wmlscript_function(number1, number2)

If a function does not require any arguments, you still need to include the parentheses (), like this:

function wmlscript_function()

The return Statement

The "return (some_value);" statement is used to return a value back to the calling function in WMLScript. For example, the calculateSum() function below returns the sum of two numbers back to the calling function each time it is executed:

function calculateSum(number1, number2)
  return (number1 + number2);

The wmlscript_function() function below calls calculateSum() with two arguments 1 and 2. The value 3 is returned from calculateSum() and is assigned to the sum variable:

function wmlscript_function()
  sum = calculateSum(1, 2);

It is not a must to include a return statement in a WMLScript function. If no return statement is included, the default value, which is an empty string, will be returned.

Page 7 of 71 Previous Page | Next Page
Contents at a Glance
Print this Web Page | | Back to Top

Feedback Form (Expand)

What do you think about this web page?
