---
page_title: 'Plugin Development - Framework: Dynamic Function Return'
description: >-
  Learn the dynamic function return type in the provider development framework.
---

# Dynamic Function Return

<Tip>

Static types should always be preferred over dynamic types, when possible.

Developers creating a function with a dynamic return will need to have extensive knowledge of the [Terraform type system](/terraform/language/expressions/types) to understand how the value type returned can impact practitioner configuration.

Refer to [Dynamic Data - Considerations](/terraform/plugin/framework/handling-data/dynamic-data#considerations) for more information.

</Tip>

Dynamic function return can be **any** value type from function logic. Set values in function logic with the [framework dynamic type](/terraform/plugin/framework/handling-data/types/dynamic).

## Function Definition

Use the [`function.DynamicReturn` type](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/function#DynamicReturn) in the [function definition](/terraform/plugin/framework/functions/implementation#definition-method).

In this example, a function definition includes a dynamic return:

```go
func (f ExampleFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
    resp.Definition = function.Definition{
        // ... other Definition fields ...
        Return: function.DynamicReturn{
            // ... potentially other DynamicReturn fields ...
        },
    }
}
```

### Custom Types

You may want to build your own data value and type implementations to allow your provider to combine validation and other behaviors into a reusable bundle. This helps avoid duplication and ensures consistency. These implementations use the `CustomType` field in the return type.

Refer to [Custom Types](/terraform/plugin/framework/handling-data/types/custom) for further details on creating provider-defined types and values.

### Documentation

Return documentation is expected in the top-level function documentation. Refer to [function documentation](/terraform/plugin/framework/functions/documentation) for information about the `Summary`, `Description`, and `MarkdownDescription` fields available.

## Setting Return Data

The [function implementation](/terraform/plugin/framework/functions/implementation) documentation covers the general methods for setting function return data in function logic.

When setting the value for this return:

* If `CustomType` is set, use its associated value type.
* Otherwise, use the [framework dynamic type](/terraform/plugin/framework/handling-data/types/dynamic).

In this example, a function defines a dynamic return and sets its value to a string:

```go
func (f ExampleFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
    resp.Definition = function.Definition{
        // ... other Definition fields ...
        Return: function.DynamicReturn{},
    }
}

func (f ExampleFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
    // ... other logic ...

    // hardcoded value for example brevity
    result := types.DynamicValue(types.StringValue("hello world!"))

    resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, &result))
}
```

For more detail on working with dynamic values, see the [framework dynamic type](/terraform/plugin/framework/handling-data/types/dynamic) documentation.
