Functions – Codeblocks
The function code blocks include two primary function block types, as well as a function conditional block. Functions are self contained modules of code that are set up to accomplish a specific task.
Functions are called from another calling block and run on demand when called.
Often a function is handed data from the calling block. At the time that it is called.
The function can be called throughout the rule as many times as needed.
It can even be handed different data each time it is called.
Here, we will create a function to set the area parameter value.
First, the block is added to the canvas, and then the name of the function is typed in.
Upon adding the function, The calling block is automatically created and added to the function blocks.
The calling block is added to the canvas at the point from which it is to be called. Next, the plus sign is clicked in the function to add a variable.
This variable is automatically added to the calling block as well. In effect, wiring up the connection between the two blocks.
Using the plus sign again, A second variable is added.
Then the parameters are plugged into the calling block so that the values are passed to the function using the variables as the means with which to do so.
Then the area parameter is added to the function, and a math block is snapped to it. Providing the place to do the calculation of the data that is to be passed into the function when it is called.
Looking in the variables, we find that our variable blocks have been automatically created there for us.
Adding them to the math block within the function, is all we need to do to complete the function to set the area value.
Testing with the form shows that the area value updates as expected and that our function works as designed.
Next, we'll add another function using the other available function code block type. Which returns an output.
First, we collapse the existing function blocks to make space in the canvas.
Next, the block is added to the canvas, and then the name of the function is typed in. The calling block, which was created for us automatically, is added to the Canvas as well.
This type of function block expects a parameter to call the function and receive the function output. So one is selected and joined to the calling block.
Next, the plus sign is clicked in the function to add variables.
Record these variables are automatically added to the calling block as well, providing the data connections between the two blocks.
A new variable is created to provide a container for the information that will be assembled before passing it back up to the calling block.
The variable is placed in the function, and then a text block is added to concatenate the label string passed in from the parameter a static string and a calculated value that converts the past in parameter value from feet to inches.
The create text with block can be expanded to add as many lines as needed to build the string just as you prefer to see it.
In this case, we will use this string as the tooltip message in the form to provide our user a quick way to see the parameter value in an alternate unit.
Then we'll add the container variable to be returned from the function back up to the calling block.
And then finally, we add the parameters to the calling block to pass the information to our completed function.
Ensuring it has the data type it expects.
A quick test in the form, and we see that it works.
Duplicating the calling block and updating the new instance to use the height parameter rather than the length, is all that is needed to reuse our function.
A quick snap to tidy up and then we can test some values in the form to ensure that our function is serving up the feet to inches conversion for both parameters that are calling it.
In summary, functions are self contained reusable modules of code, that we set up to accomplish a task.
Functions are called from a calling block and run on demand when called.
Functionsions are often handed data from the calling block to perform the task they were created to do.