If your mivascript app changes over time during upgrades and whatnot, sometimes a function might need to return more info than you've planned for. If it's a function that you're using all over the place, adding parameters or changing the return value
can cause more problems than is worth it.
One way to solve this is to assign global variables, but it's generally frowned upon as far as coding goes because you don't always know what you might be messing up somewhere else by using global variables.
Passing a Variable by ReferenceOne of the best ways to get around this is to add a VAR style parameter - reference variable - to a function that can be used as a "catch all" for data.
Here is a function that takes a character input and returns it after changing it to lower case. Yes, I know there's a built in function for this, but it's a good way to demonstrate this.
<MvFUNCTION NAME = "lowercase" PARAMETERS = "character" STANDARDOUTPUTLEVEL="">
<MvASSIGN NAME = "l.return" VALUE = "{ tolower(l.character) }" />
<MvFUNCRETURN VALUE = "{ l.return }" />
</MvFUNCTION>
Super simple, right? It takes a character then returns it after changing it to lower case.
However, what if you want to know if it's a letter A? If you are calling this function in your mivascript more than once, you don't want to change the return value because then you'll need to change it in other places in your code which is a good
way to introduce bugs.
Instead, let's pass a variable by reference using a VAR
style parameter in our function and use it to let us know if the character is a vowel.
<MvFUNCTION NAME = "lowercase" PARAMETERS = "character,info var" STANDARDOUTPUTLEVEL="">
<MvASSIGN NAME = "l.return" VALUE = "{ tolower(l.character) }" />
<MvIF EXPR = "{ 'A' CIN l.character }">
<MvASSIGN NAME = "l.info:a" VALUE = "{ 'yes' }" /> </MvIF>
<MvFUNCRETURN VALUE = "{ l.return }" />
</MvFUNCTION>
Now when you call the function like this:
<MvASSIGN NAME = "l.old_char" VALUE = "{ 'A' }" />
<MvASSIGN NAME = "l.new_char" VALUE = "{ lowercase(l.old_char,l.info) }" />
You can then evaluate the l.info:a
structure to see the data that was assigned, in this case the string yes
.
<MvEVAL EXPR = "{ l.info:a }" />
The beauty of this method is that you can keep on adding more data to the l.info
structure without changing anything else in your miva script module or app which saves you from any additional chance of introducing bugs.
Not all functions need something like this, but if you have any itch that a function you are creating may need to return more information but you're not sure what it is, using this method can save you a lot of headaches.
Related Posts and Information