|
|
|
|
TechNotes
MioScript Reference
Products
|
|
TechNote MioScript: Functions | Language: MioScript
|
| | Functions are blocks of code you can call from another code.
Functions are used to avoid repetitive code and to make the source code easier to read.
To declare a function:
function.myFunction
...
function.end
|
The function can be called from an event or another function as follow:
To return a value:
function.myFunction
...
return(&valueToReturn)
function.end
|
And to retrieve the returned value of a function:
&myVariable = myFunction()
|
For more information: function.functionName, function.end, return
|
Passing Parameters| |
Parameters are values you can send to a function.
The parameters can be sent by value (easier way) or by reference (if the function needs to update the content of a variable).
By Value
Parameters can be passed to the function as follow:
event.load
...
myFunction(&myParam1, &myParam2)
event.end
function.myFunction(&getParam1, &getParam2)
...
function.end
|
By Reference
Variables can also be passed by reference. The name of the variable is sent instead of it's value, so the function can update the content of the variable.
To send a variable by reference, proceed as follow:
event.load
...
&myVariable = "Hello"
myFunction(myVariable)
event.end
function.myFunction(&variableName)
alert(&_variableName)
function.end
|
The underscore is used to retrieve the value of the variable.
You can also set the value of the variable as follow:
&_variableName= "Good-bye"
|
The value of myVariable is set to Good-Bye.
As &variableName = myVariable, the two following lines are equivalents:
&_variableName= "Good-bye"
&myVariable= "Good-bye"
|
| Keywords in functions names| |
It is possible to add keywords in a function name.
Here is a declaration of a function with a keyword:
function.myFunction:keyword
...
return(&valueToReturn)
function.end
|
To call this function:
You can add as many keyword as you want. This can help to group functions by family.
|
Passing a function as a parameter| |
Passing a function to a function as a parameter can save many lines of code.
In the following example, a function is passed as an argument to a function.
event.load
callAndShowResult(getHello)
app.close()
event.end
function.callAndShowResult(&function_name)
_&function_name()
function.end
function.getHello()
alert("Hello")
function.end
|
The function is called with an underscore behind the variable name.
Important: It is not possible to retrieve a return value when calling a function with a variable.
|
|
|
|