Creating Functions
On the Type details page, hover your mouse pointer over the Functions option in the Blocks section of the page and click the plus button beside it.
Input the name of the function. In our example we used “newProduct”, select the data type, and click Create. This will open the blocks interface and you will see the basic function you have created.
The function you have created can be modified to suit your needs. Notice that the function definition block has a gear icon on a blue background in the left corner. When clicked, this icon presents a display for adding parameters to the block.
The top part of the following figure shows this display. The left side of the display contains input name x and @ input name (for collections) blocks that denotes a generic parameter with the name x. The right part of the display contains an inputs block with a slot into which the parameter can be plugged and renamed. The bottom part of the following figure shows that the generic parameter block can be dragged and inserted into the inputs slot and the generic name x can be changed. In this case, the parameter name was changed to “name”. To complete the function definition, just click on the gear icon again to close the display.
There is an additional slot named return on the function block. When the function completes, it is the value of the variable in this slot that is returned by the function. To display this block, check the has return box on the inputs block. Note that if you do not check the has return box, then you need connect the void block to the return type slot.
Now, define the data type of the function’s return type. Click on the Objects button and navigate to the Data types tab and select the data type you want to use. Then, connect it to the return type slot.
Set the data type that can be attached to the function. In our example we used String and Decimal (2). To do this, click on the Objects button, navigate to the Data types tab and select the one you want to use. Then, connect them to the product and price slots respectively.
The next step is to set the function’s behaviour. In this example, we want to return object with type product from string and decimal (name and price). So, click on the Create button and select Product, then rename the variable to “product”. Once this is done, drag and drop it in the function’s body.
Now, assign or pass values to the variable. To do this, right-click on the variable and select Show “product” variable. In the next window, go to the Variables tab, select Name under the product section and click Set. Follow the same steps, but this time select price and click Set. Once the blocks are created, connect them to the function.
To get the values for the variable, click on Objects and select name in the next window, then click Get. follow the same procedure but this time select price. Now, connect them to their corresponding blocks.
The function is completed by attaching the return value. The image below shows the completed version of the function.