This is an old revision of the document!


User Defined Function

User Defined Functions can improve the re-usability of your mscripts. User defined functions are first declared in function trigger in the model. There is no limit on the number of user functions you may define.

Below is an example of a user function:

testoptimal.com_img_userfunc.jpg


Declare User Defined Function

You can create as many user defined functions as you want. Each user defined function is declared with func tag that has a name attribute .

<func name="func1">
    <log msg="inside func1: parameters are $listParam()"/>
    <action code="$setFuncReturn('return value')"/>
</func>
 

User defined function can take 0 or any number of parameters. Parameters are specified as key=value pairs. Multiple parameters are passed in separately, e.g.

$callFunc('func1','lastName=smith','firstName=john')

Within the func tag, you add any mscript tags and methods. You can access the parameters passed into the function with mscript method $getFuncParam('lastName').


Return Value

Optionally you can return a value. You can do so with mscript method $setFuncReturn('returnValue'). Multiple calls to this method overrides the return value and only the value of the last call is returned.

Below is an example of a user defined function that returns a full name:

<func name='getFullName'>
    <action code="$setFuncReturn('$getFuncParam('lastName'), $getFuncParam('firstName')')"/>
</func>

Call User Defined Function

To call this user defined function, use mscript method $callFunc('funcName','param1','param2'). For example:

<log msg="Full Name is: $callFunc('getFullName','lastName=Smith','firstName=John')"/>
   

Accessing Parameters

Inside the user function, you can access the input parameters with $getFuncParam('paramName'). For example $getFuncParam('lastName') to retrieve parameter 'lastName' which should return 'Smith' in the above example.

User function may call another user function with $callFunc(…) using the above calling syntax. Be sure that you don't cause infinite recursive loop.