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
doSomething
label, change it tofibonacci
: - 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 name
block into inputs, then change the name of the input fromx
toposition
as 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 forposition - 1
and add it to the result forposition - 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
MathService
for the Service Name field and click SAVE. - In the New Codeless Method popup enter
calculateFib
for the method name andN
for the parameter 1 field. The type of the parameter must be Number. Click SAVE: - Click EDIT to edit the codeless logic for the
calculateFib
method: - In the Business Logic Designer drag the
fibonacci
block from the Custom Functions category and attach it to the return connector of thecalculateFib
method. Drag theMethod Argument N
block from Context Blocks and attach it to the position connector of thefibonacci
block. 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
N
and 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.