Quick Start Guide¶
This guide will walk you through creating a reusable function which calculates a Fibonacci number for its sequential position. You will learn how to declare a function, add an input parameter, add Codeless logic and re-use the function.
- Login to your Backendless application and click the Business Logic icon.
- Click the CODELESS tab and click Add New under Functions in the BROWSER panel.
- When visiting the Function Library section for the first time, a new function placeholder is automatically created for you. In the function block, click the
doSomethinglabel, change it to
- The function will have one input, which identifies the position of the Fibonacci number to calculate. To add the input, click the gear icon in the upper left corner of the Function block. Drag the
input nameblock into inputs, then change the name of the input from
positionas shown in the video below. Notice the declared input is automatically added to the Context Blocks area:
- The function developed in this guide will return a value (the actual Fibonacci number), however, when you develop other functions which do not need to return a value, you can control it through the interface behind the "gear" icon:
- The algorithm which will be implemented in the function assumes that the first two numbers in the sequence are 1 and 1. All other numbers are the sum of the previous two. Thus the logic of the algorithm is going to be:
If the position argument is less than or equals 1, then the function should return 1. Otherwise, the function should recursively get the result for
position - 1and add it to the result for
position - 2.
- Since the logic of the function will use recursion (the function will call itself), make sure to click SAVE so that the function shows up in the Custom Functions category:
- The final logic should look as shown below. Notice the function is re-used recursively:
The video below demonstrates the process of composing the logic for the function:
- When finished composing the logic, click the SAVE button. At this point the function implementation is complete. To see the function in action, you will create an API Service which uses the function. Click CLOSE and switch to the API Services screen.
- Click the "+" icon to create a new service and select the CODELESS tab. Enter
MathServicefor the Service Name field and click SAVE.
- In the New Codeless Method popup enter
calculateFibfor the method name and
Nfor the parameter 1 field. The type of the parameter must be Number. Click SAVE:
- Click EDIT to edit the codeless logic for the
- In the Business Logic Designer drag the
fibonacciblock from the Custom Functions category and attach it to the return connector of the
calculateFibmethod. Drag the
Method Argument Nblock from Context Blocks and attach it to the position connector of the
fibonacciblock. Click DEPLOY to deploy the logic.
- At this point the service method is ready. It uses the custom function and can be tested with real data. Click the CLOSE button in the Business Logic Designer. Click the PARAMETERS tab. Enter a value for the argument
Nand click INVOKE. Backendless Console invokes the service and displays the result. For example, the screenshot below shows that the Fibonacci number for the 10th position in the sequence is 89:
- This concludes the guide. You have developed a custom reusable function and built an API service which uses the function.