5. Defining WMLScript Functions

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);]
}


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


5.2. 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:

And here are some examples of invalid function names:


5.3. 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()
{
...
}


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


Previous Page Page 7 of 71 Next Page


Feedback Form (ExpandCollapse)

What do you think about this web page?






(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Please enter again to confirm:

Due to the amount of messages we received, we may not be able to reply to all messages.

A button for going back to the top of this page