---
schema_version: "1"
id: df05bec5fbe466b691dbfab0bd4eda7c7328d9f4c2bfed8a425e1323ebca1521
title: Define a Custom Addition Function
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.3/dataweave-cookbook-define-a-custom-addition-function.md
canonical_url: https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-define-a-custom-addition-function
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 15b70063cf03ce325fbfed3054cde89268e84c42aafbefeb35362d899ee1a750
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
---
product: DataWeave
version: 2.3
keywords: studio, anypoint, transform, transformer, format, xml, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping
---

# Define a Custom Addition Function

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

This DataWeave example takes in a set of item prices and stock amounts and uses several functions to
calculate subtotals, deduct discounts and add taxes.

It makes use of the following:

- `map` function to go through all the items in the input.

- basic math operations like `*`, `+` and `-`.

- String concatenation with `++`.

- `as` to force string values into numbers.

- Fixed values in the header to set tax and discount amounts.

- A custom function in the header, to define once and use multiple times in the code.

- `reduce` function to aggregate the various items into a total.

DataWeave Script:

```dataweave
%dw 2.0
output application/json
var tax = 0.085
var discount = 0.05
fun getSubtotal (items) = items reduce ((item, accumulator = 0) ->
        accumulator + (item.unit_price * item.quantity * (1 - discount)))
---
invoice: {
    header: payload.invoice.header,
    items: { (payload.invoice.items map {
        item : {
            description: $.description,
            quantity: $.quantity,
            unit_price: $.unit_price,
            discount: (discount * 100) as Number ++ "%",
            subtotal: $.unit_price * $.quantity * (1 - discount)
        }
    }) },
    totals:
    {
        subtotal: getSubtotal(payload.invoice.items ),
        tax: (tax * 100) as Number ++ "%",
        total: getSubtotal(payload.invoice.items ) * (1 + tax)
    }
}
```

Input JSON Payload:

```json
{
  "invoice": {
    "header": {
      "customer_name": "ACME, Inc.",
      "customer_state": "CA"
    },
    "items": [
      {
        "description": "Product 1",
        "quantity": "2",
        "unit_price": "10"
      },
      {
        "description": "Product 2",
        "quantity": "1",
        "unit_price": "30"
      }
    ]
  }
}
```

Output

```json
{
  "invoice": {
    "header": {
      "customer_name": "ACME, Inc.",
      "customer_state": "CA"
    },
    "items": {
      "item": {
        "description": "Product 1",
        "quantity": "2",
        "unit_price": "10",
        "discount": "5%",
        "subtotal": 19.0
      },
      "item": {
        "description": "Product 2",
        "quantity": "1",
        "unit_price": "30",
        "discount": "5%",
        "subtotal": 28.5
      }
    },
    "totals": {
      "subtotal": 47.5,
      "tax": "8.500%",
      "total": 51.5375
    }
  }
}
```

## Related Examples

- [Map Data](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-map)

- [Use Constant Directives](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-use-constant-directives)

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

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

- [Pass Functions as Arguments](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-pass-functions-as-arguments)

## See Also

- [DataWeave Core Functions](https://docs.mulesoft.com/dataweave/2.3/dw-core)

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

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