---
schema_version: "1"
id: 2298712a02b3d6b564d9e0947aa1608aaaf7519fdc03ebe0bd93981951ce1cd6
title: Work with Functions and Lambdas in DataWeave
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/latest/dataweave-functions-lambdas.md
canonical_url: https://docs.mulesoft.com/dataweave/latest/dataweave-functions-lambdas
source_site: docs.mulesoft.com
content_type: documentation
content_hash: b0f314e5ecd9b5b3a208a79a10f82da65fc2df1d74cc0a57fde3c720b89367cc
description: MuleSoft Documentation Site
release: "2.13"
latest: true
---
---
product: DataWeave
version: 2.13
is-latest-version: true
keywords: studio, anypoint, esb, 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
---

# Work with Functions and Lambdas in DataWeave

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

In DataWeave, functions and lambdas (anonymous functions) can be passed as values or be assigned to
variables.

When using lambdas within the body of a DataWeave file in conjunction with an function such as
`map`, its attributes can either be explicitly named or left anonymous, in which case they can be
referenced as `$`, `$$`, etc.

## Declare and Invoke a Function

You can declare a function in the header or body of a DataWeave script by using the `fun` keyword.
Then you can invoke the function at any point in the body of the script.

You refer to functions using this form: `functionName()` or `functionName(arg1, arg2, argN)`

You can pass an expression in between the parentheses for each argument. Each expression between the
parentheses is evaluated, and the result is passed as an argument used in the execution of the
function body.

Input

```json
{
  "field1": "Annie",
  "field2": "Point",
  "field3": "Stuff"
}
```

Transform

```dataweave
%dw 2.0
output application/json
fun toUser(obj) = {
  firstName: obj.field1,
  lastName: obj.field2
}
---
{
  "user" : toUser(payload)
}
```

Output

```json
{
  "user": {
    "firstName": "Annie",
    "lastName": "Point"
  }
}
```

## Specify Type Parameters

Starting in DataWeave syntax version 2.5, you can specify the type parameters of a function at the
call site.

Input

```json
{
  "measures": [1, 2, 4, 1, 5, 2, 3, 3]
}
```

Transform

```dataweave
%dw 2.5
output application/json

fun max<T>(elems: Array<T>): T = elems reduce ((candidate: T, currentMax = elems[0]) -> if (candidate > currentMax) candidate else currentMax)
---
{
  max: max<Number>(measures)
}
```

Output

```linenums
{
  "max": 5
}
```

## Assign a Lambda to a Var

You can define a function as a variable with a constant directive through `var`

Input

```json
{
  "field1": "Annie",
  "field2": "Point",
  "field3": "Stuff"
}
```

Transform

```dataweave
%dw 2.0
output application/json
var toUser = (user) -> {
  firstName: user.field1,
  lastName: user.field2
}
---
{
  "user" : toUser(payload)
}
```

Output

```json
{
  "user": {
    "firstName": "Annie",
    "lastName": "Point"
  }
}
```

## Use Named Parameters in a Lambda

This example uses a lambda with an attribute that is explicitly named as `name`.

Input

```dataweave
%dw 2.0
output application/json
var names = ["john", "peter", "matt"]
---
users: names map((name) -> upper(name))
```

Transform

```json
{
  "users": ["JOHN", "PETER", "MATT"]
}
```

## Use Anonymous Parameters in a Lambda

This example uses a lambda with an attribute that’s not explicitly named, and so is referred to by
default as `$`.

Transform

```dataweave
%dw 2.0
output application/json
var names = ["john", "peter", "matt"]
---
users: names map upper($)
```

Output

```json
{
  "users": ["JOHN", "PETER", "MATT"]
}
```

## See Also

[Supported Data Formats](https://docs.mulesoft.com/dataweave/latest/dataweave-formats)

[DataWeave Scripts](https://docs.mulesoft.com/dataweave/latest/dataweave-language-introduction)
