> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-framework/interceptors/core-interception-points/function-invocations.md).

# Function Invocations

These events occur when a user-defined function is about to be executed or has finished executing. They are announced on the **global interceptor pool**.

> **Performance guard:** `preFunctionInvoke`, `postFunctionInvoke`, and `onFunctionException` are only announced when at least one listener is registered for any of the three events. If no listeners exist, this code path is skipped entirely. Keep function interceptors lightweight as they fire on every user-defined function call.

| Event Name            | Cancellable | Description                                                                                      |
| --------------------- | :---------: | ------------------------------------------------------------------------------------------------ |
| `preFunctionInvoke`   |      No     | Fired before a user-defined function executes.                                                   |
| `postFunctionInvoke`  |   **Yes**   | Fired after a function returns successfully. Interceptors can override the return value.         |
| `onFunctionException` |      No     | Fired when a function throws an exception. The exception is re-thrown after the event completes. |

* [`preFunctionInvoke`](#prefunctioninvoke) - This event is triggered before a function is invoked.
* [`postFunctionInvoke`](#postfunctioninvoke) - This event is triggered after a function has been invoked.
* [`onFunctionException`](#onfunctionexception) - This event is triggered when an exception occurs during the function invocation.

## preFunctionInvoke

This event is triggered before a function is invoked. It allows you to perform actions or modifications before the function execution begins. The arguments scope has already been created and populated at this point, so arguments can be inspected or mutated before the function body runs.

### Data Structure

| Data Key    | Type                 | Description                             |
| ----------- | -------------------- | --------------------------------------- |
| `arguments` | `ArgumentsScope`     | The arguments scope                     |
| `context`   | `FunctionBoxContext` | The function-specific context           |
| `function`  | `Function`           | The UDF/Function/Closure/Lambda invoked |
| `name`      | `String`             | The name of the function invoked        |

### Example

```groovy
class myListener{

	function preFunctionInvoke( struct data ){
		// Access the function name
		var functionName = data.name;

		// Log the function invocation
		log.info("Function invoked: " & functionName);

		// You can modify arguments if needed
		data.arguments.set("newArg", "value");
	}
}
```

## postFunctionInvoke

This event is triggered after a function has returned **successfully**. Does not fire if the function threw an exception — use `onFunctionException` for that. The same event data struct from `preFunctionInvoke` is reused with `result` added.

The `result` key is **mutable** — setting it in the event data overrides the value returned to the caller.

### Data Structure

| Data Key    | Type                 | Description                                                                                                               |
| ----------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------- |
| `arguments` | `ArgumentsScope`     | The arguments scope                                                                                                       |
| `context`   | `FunctionBoxContext` | The function-specific context                                                                                             |
| `function`  | `Function`           | The UDF/Function/Closure/Lambda invoked                                                                                   |
| `name`      | `String`             | The name of the function invoked                                                                                          |
| `result`    | `Any`                | The return value of the function (absent if the function returned `null`). **Set this key to override the return value.** |

### Example

```groovy
class myListener{
	function postFunctionInvoke( struct data ){
		// Access the function name
		var functionName = data.name;

		// Log the function invocation result
		log.info("Function invoked: " & functionName & ", Result: " & data.result);

		// You can modify the result if needed
		data.result = "Modified Result";
	}
}
```

## onFunctionException

This event is triggered when an exception occurs during the function invocation. The exception is **not suppressed** — it is re-thrown after the event completes. The same event data struct from `preFunctionInvoke` is reused with `exception` added.

### Data Structure

| Data Key    | Type                 | Description                                                |
| ----------- | -------------------- | ---------------------------------------------------------- |
| `arguments` | `ArgumentsScope`     | The arguments scope                                        |
| `context`   | `FunctionBoxContext` | The function-specific context                              |
| `function`  | `Function`           | The UDF/Function/Closure/Lambda invoked                    |
| `name`      | `String`             | The name of the function invoked                           |
| `exception` | `Throwable`          | The exception that occurred during the function invocation |

### Example

```groovy
class myListener{
	function onFunctionException( struct data ){
		// Access the function name
		var functionName = data.name;

		// Log the exception
		log.error("Exception in function: " & functionName & ", Message: " & data.exception.message);

		// Note: the exception will still be thrown after this event completes.
	}
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://boxlang.ortusbooks.com/boxlang-framework/interceptors/core-interception-points/function-invocations.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
