UIEvolutin Inc.
UJML Language Reference
Returning Function Results

Functions may return the results of a calculation to the calling code.

A user-defined function may have a return value, defined by the function type attribute. If the type is 'void', the function returns nothing and no return element is required in the function declaration. If the type is a UJML data type, the function must return a value of that data type and the return element is required in the declaration. See Data Types

The value returned by the function may be computed using a script expression in the return element or by scripting statements in the script element of the function declaration. See Scripting, Script Expressions.

The following example shows how to calculate a random value within a range, given the range and a seed value. The calculation is an expression in the return element and no script element is needed. It is part of the math.ujml sample.

<function name="randRange" type="int">
    <parameters>
        <var name="seed" type="int"/>
        <var name="min" type="int"/>
        <var name="max" type="int"/>
    </parameters>
    <return>
        <eval>
            (_srand(seed) % (max - min)) + min
        </eval>
    </return>
</function>

 

The following example shows how to return the value of a private variable. It requires no parameters or script elements. It is part of the datetime.ujms sample.

<function name="getSecond" type="int" visibility="public">
    <return><eval>second</eval></return>
</function>

 

The following example shows how to find the first occurrence of a Unicode code point in the passed string. It uses function variables and scripting, returning the value of a function variable, defaulting to -1 if the code point is not found. It is part of the strings.ujml sample.

<function name="indexOf" type="int">
    <parameters>
        <var name="target" type="string"/>
        <var name="char" type="int"/>
    </parameters>
    <variables>
        <var name="posFound" type="int"/>
        <var name="pos" type="int"/>
        <var name="targetLength" type="int"/>
    </variables>
    <script>
        // Assume failure.
        posFound = -1;
        targetLength = _strlen(target);

        // Linear search for character codepoint.
        for (pos = 0; pos &_LT; targetLength; pos++)
        {
            if (char == _getCharacterCode(target, pos))
            {
                posFound = pos;
                pos = targetLength;
            }
        }
    </script>
    <return><eval>posFound</eval></return>
</function>
Copyright (c) 2000-2007 UIEvolution, Inc. All rights reserved.
What do you think about this topic? Send feedback!