---
schema_version: "1"
id: 16a4c105c6c5ed60464c4a56037cd1884fa8cce0e809e5db11d0b2606193f8d8
title: Pass Functions as Arguments
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.6/dataweave-cookbook-pass-functions-as-arguments.md
canonical_url: https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-pass-functions-as-arguments
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 757864cb99154c22caf63527d1212e32f923469ce2d532ad1c62e50ae69073e3
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
---
product: DataWeave
version: 2.6
keywords: studio, anypoint, transform, transformer, format, aggregate, rename, split, filter convert, xml, json, csv, pojo, java object, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping
---

# Pass Functions as Arguments

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

This DataWeave example defines a base function in the header that receives two arguments: a function
and an element to apply it on. The base function the applies the received function onto the keys of
the received element and also to every one of its child elements recursively. In this case, the
function sent sets all keys to lower case.

The example uses the following:

- A custom function that is applied recursively, ie: it calls itself for each child in the element
  it receives originally.

- `mapObject` to sort through each child of the element.

- `lower` from the Strings library to make every Key in the input lower case.

- `is` to check if the passed element is of type `Object` (and therefore has children).

- `if` and `else` to only have the function call itself when a condition is met, and to otherwise
  end the recursive loop for that branch.

DataWeave Script:

```dataweave
%dw 2.0
import * from dw::core::Strings
output application/xml
fun applyToKeys(element, func) =
    if (element is Object)
        element mapObject (value, key) -> {
        (func(key)) : applyToKeys( value, func)
        }
    else element
---
applyToKeys(payload, (key) -> lower(key))
```

Input XML Payload:

```xml
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
</CATALOG>
```

Output XML:

```xml
<?xml version='1.0' encoding='US-ASCII'?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
</catalog>
```

## Related Examples

- [Define a Custom Addition Function](https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-define-a-custom-addition-function)

- [Dynamically Map Based On a Definition](https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-map-based-on-an-external-definition)

- [Define a Function that Flattens Data in a List](https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-define-function-to-flatten-list)

- [Conditionally Reduce a List through a Function](https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-conditional-list-reduction-via-function)

## See Also

- [DataWeave Selectors](https://docs.mulesoft.com/dataweave/2.6/dataweave-selectors)

- [DataWeave Cookbook](https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook)
