# Global Functions

Global Functions in eMOBIQ allow users to create and manage their own custom functions that can be reused across multiple pages and components. Global functions are user-defined logic blocks built with other logic blocks in eMOBIQ. These functions can be used in the visual logic and provide a convenient and efficient way to add event-driven logic based on specified requirements.

## Creating a Global Function

To create a global function, follow these steps:

1. Access the "Global Functions" section and click on "Add Global Function". A pop-up will appear asking for the name and result global variable name for the function.
2. After the function is created, you can modify the global function name and result global variable name in the inspector if necessary.

Note:

In the inspector, 'Parameters' specify the parameters that the function will take as inputs when you call it in the visual logic.

'Result' specifies the global variable name that represents the result of the function. If there is no result global variable name defined, then the return value from a global function will be 'false' by default.

3. The function's implementation is displayed on the visual logic interface, where you can easily edit and customize the function according to your needs.

## Using Arguments in Global Functions

When you define parameters for a global function, these parameters act as the inputs to the function. To use the values passed into these parameters as inputs to other functions within the global function, you must specify that these parameters are of 'param' type.

For example, if a global function has parameters p1 and p2, to use the values passed into p1 and p2 as inputs to other functions within the global function, you have to specify that p1 and p2 are 'param' types.

In the example below, p1 and p2 are used as inputs of 'param' type to the `add` logic block within the global function:

![Param Example](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-ef508713678932677df78997f0d62efff4fe9b77%2Fparam-example.png?alt=media)

## Using the Return Value of Global Functions

To retrieve the return value of global functions, you can do so by using the `getVar` function on the global variable name associated with the global function result. However, do take note of asynchronous behaviours when using asynchronous functions.

Alternatively, you can also use the global function as a subflow of another function and get the result directly as an input value to the caller function. ([See example below](#example-using-a-global-function))

## Example: Creating a Global Function

In this example, we will create a global function that takes two numbers as inputs and returns their sum.

1. Access the "Global Functions" section and click on "Add Global Function". A pop-up will appear asking for the name and result global variable name for the function. Enter the name as "AddNumbers" and the result global variable name as "AddNumbersResult".

   ![Adding global function](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-70a32cf9c96d63a89c52ff7307e54c99d1c06386%2Fadd-global-function.png?alt=media) ![Pop-up to enter function name and result global variable name](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-f29ee2579a4389bfbbff65f7d490914934421b12%2Fadd-global-function-2.png?alt=media)
2. After the function is created, you can modify the global function name and result global variable name in the inspector if necessary.

   ![Global Function Inspector](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-4ae5218f0ffc817b92bc58cda74f3df0f2e27c4d%2Fglobal-function-inspector.png?alt=media)
3. In the global function inspector, add two new parameters: `number1` and `number2`.

   ![Screenshot showing the parameters defined in the inspector](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-c42d405725877df0772c5c9fb7357d4b1d16b260%2Fparameters.png?alt=media)
4. In the visual logic interface, add logic blocks to add the two parameters and assign the result to the "AddNumbersResult" global variable.

   ![Screenshot showing the visual logic interface](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-3cdbe588cfd329bb2606c40d8f165a600401e80f%2Flogic-parameters-add.png?alt=media)

   Note:

   Within the visual logic, to use values passed into the parameters of the global function, you need to use the 'param' parameter type.
5. Finally, assign the result to the result global variable, "AddNumbersResult".

   ![Screenshot showing the visual logic interface](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-0f37925db4d3f3f539d43343009817678c54fb49%2Fsetting-results.png?alt=media)

   In this example, we use the `add` function as a subflow to the `setVar`'s value parameter. This means that the return value from the `add` function will be directly given to `setVar` function's value parameter.

Now, the "AddNumbers" global function is created and can be used in the visual logic.

## Example: Using a Global Function

In this example, we will use the "AddNumbers" global function created in the previous example.

1. In the visual logic interface, add the "AddNumbers" logic block.

   !["AddNumbers" logic block](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-727815e529e95a0a213f3117241f5ee60790061b%2Fdemo-block.png?alt=media)
2. Set the 'number1' and 'number2' parameters in the 'call function' logic block.

   ![Screenshot showing the parameters set in the 'call function' logic block](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-6d20993ad58c1020fcb5c2c5904bc2555c9a8963%2Fdemo-block-inspector.png?alt=media)
3. Retrieve the result of the "AddNumbers" function either using the `getVar` function to retrieve the "AddNumbersResult" global variable, or using "AddNumbers" as a direct subflow for an input.

   Using the `getVar` method:

   ![Screenshot showing the 'getVar' function](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-c7b1099d5fe37ac8819b9906dda649091a2954cf%2FgetVar-result.png?alt=media)

   Using the subflow method (E.g. using the value directly for console):

   ![Subflow Example](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-f296391c46846f3a4e29ba1522fb610b6279f33a%2Fsubflow-example.png?alt=media)
4. The result can now be used in the visual logic.
