# RuLa Functions

### Definition

A RuLa function is essentially an expression that performs a task or calculation and then returns one value. The calculation is defined in the function's body. If parameters (or arguments) are required to perform the calculation, they can be passed to the function in parenthesis.

Functions are useful if a task needs to be performed repeatedly at different places in the configuration. Once defined correctly, a function can be used over and over again without having to thinking about the complicated calculation.

### Examples

Each function needs a name and a body expression and they can take none as well as one or more parameters.

#### Without parameters

```
function say_hello():
   'Hello everybody' 
end
```

This function returns whatever is stated in its body, in this case 'Hello everybody'.

```
function half_a_dozen():
  12/2
end
```

This function will return the number 6.

#### With one parameter

```
function divide_by_two(number):
   number/2 
end
```

The number that is passed to this function as a parameter is divided by two and then returned as a result. So `divide_by_two(30)` would return `15`.

#### With multiple parameters

```
function introduce(first_name, last_name):
   'My name is ' + last_name + ' but you can call me ' + first_name 
end
```

In the case of introduce(James, Bond) this would return the string 'My name is Bond but you can call me James".

Within the function's body, other functions can be used, both functions that already exist in Atfinity or the ones that were defined by the user.

```
function replace_element(some_list, element_to_replace, replacement):
   map(
      some_list, 
      if 
         CURRENT = number_to_replace then 
         replacement 
         else CURRENT 
      end
   )
end
```

This function uses the map function in its body to replace all occurrences of a specific element in that list with a specified replacement. So writing `replace_number([8, 5, 8], 8, 0)` would return `[0, 5, 0]`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.atfinity.io/guides/glossary/rula-functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
