Logic - Functions vs Actions
Introduction
Even if Functions and Actions are very close, they have each some specificities:
Functions | Actions | ||
---|---|---|---|
Usage examples | Compute text of Button, Compute position of UI Component, Compute math operation, … | Create object in database, send email, perform authentication, … | |
Executed when | All inputs are resolved (= have a value) or any input changes | The Control Flow input receives a new “pulse” (e.g. an interaction) | |
Order of execution is guaranteed | No | Yes | |
Can call other functions | Yes | Yes | |
Can call other actions | No | Yes |
Custom reusable functions & actions
Custom reusable Functions and Actions can be created in a project.
Inputs and Outputs can be added to the Function / Action:
Actions automatically come with Control Flow Inputs and Outputs:
Function and Actions can be tested using play button:
Best practices
When a function or an action becomes complex, try to create smaller functions/actions and call them from a higher level function/action.
It is a good habit to create reusable functions/actions for any logic that is not directly related to User Interface.
Exercise 9 – Reusable function
The goal of this exercise is to create a reusable function which sums 3 numbers and return the result. This exercise is not linked to the Note app and is here for training purpose.
- Go to Project “Notes”.
- Open Folder “Logic”.
- Create a new Function named “Add 3”.
- Open the new Function and complete it as follow: