10. Passing Arguments to Functions By Value and By Reference
In WMLScript, arguments are passed to functions by value, which means if you specify a variable as the argument of a function, the value of this variable will not be affected by any operation inside the function. This is because when an argument is passed by value, a copy of the variable is passed to the function instead of the original variable.
Passing arguments by reference means a reference to the variable is passed to the function instead of a copy of the variable. The value of the variable in the calling function can be changed by operations inside the called function.
WMLScript does not support passing arguments by reference. This creates a problem for us since arguments have to be passed by reference in some situations. One situation is when we want to return multiple values back to the calling function. (Remember that the return statement can only be used to return one value. So, it cannot help us in this situation.) To solve the problem, we can make use of the "passing arguments by reference" way: return values are assigned to the argument variables, whose values can be read in the calling function.
Although WMLScript does not support "pass by reference", we can use the setVar() and getVar() functions and WML variables to simulate it.
The following example demonstrates how to do this. We will take a date in the MM-DD-YYYY format (e.g. 08-30-2005) from the user and change it to a different format DD/MM/YYYY (e.g. 30/08/2005). Below is the WML document of the example:
<?xml
version="1.0"?>
<!DOCTYPE wml PUBLIC
"-//WAPFORUM//DTD WML 1.3//EN"
"http://www.wapforum.org/DTD/wml13.dtd">
<wml>
<card
id="card1" title="WMLScript
Tutorial">
<p>
Please enter a date in
the MM-DD-YYYY format:<br/>
<input
name="datef1"/><br/>
<a
href="passByRefEg1.wmls#changeDateFormat('$(datef1)')">Run
WMLScript</a><br/><br/>
</p>
<pre>$(result)</pre>
</card>
</wml>
If you view the above WML document in a mobile phone browser, you will see something similar to this:
|
|
Enter 08-30-2005 in the input field:
|
|
Select the "Run WMLScript" anchor link and the changeDateFormat() function of the passByRefEg1.wmls file will be executed. Here shows the code of the function:
extern
function changeDateFormat(date)
{
WMLBrowser.setVar("datef2",
date);
parseDate("datef2", "day",
"month", "year");
var datef2 =
WMLBrowser.getVar("datef2");
var day =
WMLBrowser.getVar("day");
var month =
WMLBrowser.getVar("month");
var year =
WMLBrowser.getVar("year");
WMLBrowser.setVar("result",
"Day: " + day + "\nMonth: " + month + "\nYear:
" + year + "\nDate after conversion: " +
datef2);
WMLBrowser.refresh();
}
Inside the changeDateFormat() function, we call another WMLScript function parseDate(). Notice that we simulate "pass by reference" here. The parseDate() function takes four WML variable names as arguments. Before parseDate() is called, the first WML variable contains the date in the MM-DD-YYYY format. When parseDate() returns, the first WML variable contains the date in the DD/MM/YYYY format and the next three WML variables contain the day value, month value and year value respectively. We will then retrieve the value of these four WML variables and print them out.
Here shows the WMLScript code of the parseDate() function:
function
parseDate(dateWMLVar, dayWMLVar, monthWMLVar, yearWMLVar)
{
var
date = WMLBrowser.getVar(dateWMLVar);
var month =
String.elementAt(date, 0, "-");
var day =
String.elementAt(date, 1, "-");
var year =
String.elementAt(date, 2, "-");
date = day + "/"
+ month + "/" + year;
WMLBrowser.setVar(dateWMLVar,
date);
WMLBrowser.setVar(dayWMLVar,
day);
WMLBrowser.setVar(monthWMLVar,
month);
WMLBrowser.setVar(yearWMLVar, year);
}
As you can see above, we make use of the String standard library's elementAt() function in parseDate(). It helps us break down a string using the specified delimiter. Details about it will be mentioned in the "Getting the Element at a Certain Index in a String: elementAt() Function" section of this tutorial. Now all you need to know is that it breaks down a date, say "08-30-2005", into "08", "30" and "2005".
The following screenshots show the result of the above example in some mobile phone browsers:
|
|
Previous Page | Page 15 of 71 | Next Page |
- 1. WMLScript Introduction
- 2. Hello World WMLScript Example
- 3. Compiling WMLScript Code
- 4. WMLScript Language Rules
- 5. Defining WMLScript Functions
- 6. Calling WMLScript Functions
- 7. WMLScript Variables
- 8. WMLScript Data Types
- 9. WMLScript Variables Vs WML Variables
- 10. Passing Arguments to Functions By Value and By Reference
- 11. WMLScript Operators
- 12. WMLScript Conditional Statements
- 13. WMLScript Looping Statements
- 14. WMLScript Standard Libraries Overview
- 15. WMLScript WMLBrowser Standard Library
- 16. WMLScript Dialogs Standard Library
- 17. WMLScript String Standard Library
- 18. WMLScript Float Standard Library
- 19. WMLScript Lang Standard Library
- 20. WMLScript URL Standard Library
- 21. WMLScript Example: Validating Form Data