Passing Arguments to Functions By Value and By Reference

Preferences - Do not show ads

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:


(passByRefEg1.wml)

<?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:



Sony Ericsson T68i


Nokia Mobile Browser 4.0


Enter 08-30-2005 in the input field:



Sony Ericsson T68i


Nokia Mobile Browser 4.0


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:


(passByRefEg1.wmls)

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:


(passByRefEg1.wmls)

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:



Sony Ericsson T68i





Nokia Mobile Browser 4.0


Page 15 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?






WMLScript Tutorial Table of Contents WMLScript Tutorial Contents at a Glance Preferences - Change Color Scheme Preferences - Do Not Show Ads Previous Page Next Page