# DataWeave modules, functions and variables

*MuleSoft Developer Study Guide · Transforming Data · Last updated October 28, 2026 · https://www.certifyforce.com/study-guide/mulesoft-developer/transforming-data/dataweave-modules-functions-and-variables*

How to build a custom .dwl module under src/main/resources and import it (module prefix, import *, named elements, aliases), how fun declarations take typed and optional parameters (including right-to-left assignment when optional parameters come first), and how global vars, do-scoped local vars and lambda vars behave.

- Certification: Salesforce MuleSoft Developer
- Exam section: Transforming Data
- Study guide: https://www.certifyforce.com/study-guide/mulesoft-developer

**Official exam objective:**

> Define, use, and reuse DataWeave modules, functions, and variables, and predict outcomes.

## What the exam expects

- Create a reusable custom module in `src/main/resources` and know what it may and may not contain.
- Import a module or its elements (`import modules::X`, `import * from`, named elements, `as` aliases) and call them with or without the `X::` prefix.
- Define functions with `fun`, type constraints and optional parameters, and predict how arguments bind.
- Predict the scope and lifetime of global `var`s, `do`-scoped local variables and lambdas assigned to variables.
- Key terms: **custom module** (a `.dwl` file of `var`, `fun`, `type` and `ns` declarations); **mapping file** (a `.dwl` file holding a complete script, called through `main`); **alias** (a local name assigned with `as` at import); **optional parameter** (a parameter with a default value); **`do` scope** (a header-and-body block that declares local variables); **lambda** (an anonymous function such as `(s) -> upper(s)`).

## Key facts

| Fact | Detail | Source |
|---|---|---|
| Module contents | Only `var`, `fun`, `type`, `ns`; no `output`, `---` or body | [Create Custom Modules and Mappings](https://docs.mulesoft.com/dataweave/latest/dataweave-create-module) |
| Module location | Under `src/main/resources`, e.g. `src/main/resources/modules/MyModule.dwl` | [Create Custom Modules and Mappings](https://docs.mulesoft.com/dataweave/latest/dataweave-create-module) |
| `import modules::M` | Calls need the prefix: `M::myFunc` | [Create Custom Modules and Mappings](https://docs.mulesoft.com/dataweave/latest/dataweave-create-module) |
| `import *` or named import | No prefix needed | [Create Custom Modules and Mappings](https://docs.mulesoft.com/dataweave/latest/dataweave-create-module) |
| Alias | `as` renames a module or element to avoid name clashes | [Create Custom Modules and Mappings](https://docs.mulesoft.com/dataweave/latest/dataweave-create-module) |
| Mapping file | Has a body; call it with `Module::main(...)` | [Create Custom Modules and Mappings](https://docs.mulesoft.com/dataweave/latest/dataweave-create-module) |
| Argument binding | Left to right; right to left when optional parameters come first or all are optional | [Define DataWeave Functions](https://docs.mulesoft.com/dataweave/latest/dataweave-functions) |
| Immutability | DataWeave variables cannot be reassigned | [DataWeave Variables](https://docs.mulesoft.com/dataweave/latest/dataweave-variables) |
| `do` scope | Local variables visible only inside the scope | [DataWeave Variables](https://docs.mulesoft.com/dataweave/latest/dataweave-variables) |
| Lifetime | DataWeave variables do not persist beyond the script and are not Mule event variables | [DataWeave Variables](https://docs.mulesoft.com/dataweave/latest/dataweave-variables) |

## Check yourself

- What does the documented `MyMapping::main(payload: { "user" : "bar" })` return?
- Why does `allParametersOptional('A')` set `param3` rather than `param1`?
- Why does a `do` scope need the `---` separator?
- What does `fun` do relative to `var` for a lambda?

## Sources

- [Create Custom Modules and Mappings](https://docs.mulesoft.com/dataweave/latest/dataweave-create-module)
- [Define DataWeave Functions](https://docs.mulesoft.com/dataweave/latest/dataweave-functions)
- [DataWeave Variables](https://docs.mulesoft.com/dataweave/latest/dataweave-variables)
- [Flow Control and Scope Operations in DataWeave](https://docs.mulesoft.com/dataweave/latest/dataweave-flow-control)
- [Pass Functions as Arguments](https://docs.mulesoft.com/dataweave/latest/dataweave-cookbook-pass-functions-as-arguments)

## About the full unit

The "Core concepts", "Exam traps" and "Worked scenario" sections of this unit, its 5 linked practice questions, and unlimited timed mock exams are included with the MuleSoft Developer pack (USD $19): https://www.certifyforce.com/practice-exams
