---
schema_version: "1"
id: c131ec169e9a4ad063ab242cefbea4ddc0a3ec6e4d811918453565fb747f645c
title: Contributing Functions with Mule SDK
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/mule-sdk/latest/functions.md
canonical_url: https://docs.mulesoft.com/mule-sdk/latest/functions
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 123ef143c1fb4e42b6146e5e71eea032d53bfc9a9fbb4ad69005020bc7c91073
description: MuleSoft Documentation Site
release: "1.13"
latest: true
---
---
product: Mule SDK
version: 1.13
is-latest-version: true
keywords: mule, sdk, functions, function
---

# Contributing Functions with Mule SDK

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

DataWeave is the main expression language in Mule 4, and the Mule SDK is also aligned with that
view. This allows you to contribute functions to DataWeave from your module’s code in a similar
fashion that you code a Mule [Operation](https://docs.mulesoft.com/mule-sdk/latest/operations). Using custom functions allows you to reuse
code in a new way, providing extra functionality in a single Module with very little overhead
regarding how they are declared compared to a Mule operation.

## Declaring a Function

At Extension level, you can use the `@ExpressionFunctions` annotation to reference the classes that
contain the public methods representing the DataWeave functions. This method’s code is executed
directly from DataWeave scripts using it name as identifier:

```java
@Extension(name = "Docs")
@ExpressionFunctions(DocsFunction.class)
public class DocsModule {

}
```

In the `DocsFunction` class, this example defines the following method:

```java
public Object xpath(@Optional(defaultValue = PAYLOAD) InputStream item,
                    String expression){
    try {
      return xPathFactory.newXPath().evaluate(expression, documentBuilder.parse(item));
    } catch (Exception e) {
      return null;
    }
  }
```

This `xpath` function can now be used from inside a DataWeave script, referencing it _inside our
Module’s namespace_.

The next example reads XML and sets an XPath filter using the new `xpath` function:

```xml
<flow name="xpathFunctionUsingPayloadAsDefault">
  <file:read path="files/bookstore.xml"/>
  <set-payload value="#[Docs::xpath("/bookstore/book[price>35]/title")]"/>
</flow>
```

To view the actual implementation of the simplified example shown above, refer to
[XPathFunction.java](https://github.com/mulesoft/mule-xml-module/blob/1.3.3/src/main/java/org/mule/module/xml/internal/xpath/XPathFunction.java).

## Restrictions: Config-less and Connection-less

By definition, functions are not allowed to depend on or receive a particular
[Configuration](https://docs.mulesoft.com/mule-sdk/latest/configs) or [Connection](https://docs.mulesoft.com/mule-sdk/latest/connections).
