> For a complete index of all SignalWire documentation pages, fetch https://signalwire.com/docs/llms.txt

# execute

> Call a named section as a subroutine and return when it completes.

Call a named section as a subroutine. Execution continues in the called section, then returns to the
caller when the section completes (via [`return`](/docs/swml/reference/messaging/return) or by
reaching the end of the section).

The destination must be the **name of a section** defined in the current document — `execute` does not
accept URLs or inline documents in the messaging context.

## **Properties**

An object that accepts the following properties.

Name of the section to execute. Must reference a section defined in the current document.

Parameters accessible as `params.*` in the called section. Replaces (does not merge with) any outer
`params` from the caller.

## **Variables**

After the called section completes, `execute` exposes the following variable in the caller's
context. Variables that the subroutine itself set via [`reply`](/docs/swml/reference/messaging/reply)
or [`request`](/docs/swml/reference/messaging/request) — `reply_result`, `reply_message_id`,
`request_result`, `request_response`, `request_response_code`, `request_response_body` — are
also propagated back to the caller automatically; see those methods for details.

The value supplied to [`return`](/docs/swml/reference/messaging/return) inside the called
section. Set only when the subroutine called `return` with an argument. Absent when the
subroutine ran to completion without `return`.

## **Examples**

### Calling a subroutine

```yaml
version: 1.0.0
sections:
  main:
    - execute:
        dest: greet
        params:
          name: "%{message.from}"
    - reply:
        body: "%{return_value}"
  greet:
    - return: "Hello, %{params.name}!"
```

```json
{
  "version": "1.0.0",
  "sections": {
    "main": [
      {
        "execute": {
          "dest": "greet",
          "params": {
            "name": "%{message.from}"
          }
        }
      },
      {
        "reply": {
          "body": "%{return_value}"
        }
      }
    ],
    "greet": [
      {
        "return": "Hello, %{params.name}!"
      }
    ]
  }
}
```

### Branch on the subroutine return value

```yaml
version: 1.0.0
sections:
  main:
    - execute:
        dest: classify
        params:
          body: "%{message.body}"
    - switch:
        variable: return_value
        case:
          opt_out:
            - reply:
                body: "You've been unsubscribed."
          help:
            - reply:
                body: "Reply STOP to unsubscribe."
        default:
          - reply:
              body: "Thanks for your message!"
  classify:
    - switch:
        variable: params.body
        transform: lowercase_trim
        case:
          stop:
            - return: "opt_out"
          help:
            - return: "help"
        default:
          - return: "other"
```

```json
{
  "version": "1.0.0",
  "sections": {
    "main": [
      {
        "execute": {
          "dest": "classify",
          "params": {
            "body": "%{message.body}"
          }
        }
      },
      {
        "switch": {
          "variable": "return_value",
          "case": {
            "opt_out": [
              {
                "reply": {
                  "body": "You've been unsubscribed."
                }
              }
            ],
            "help": [
              {
                "reply": {
                  "body": "Reply STOP to unsubscribe."
                }
              }
            ]
          },
          "default": [
            {
              "reply": {
                "body": "Thanks for your message!"
              }
            }
          ]
        }
      }
    ],
    "classify": [
      {
        "switch": {
          "variable": "params.body",
          "transform": "lowercase_trim",
          "case": {
            "stop": [
              {
                "return": "opt_out"
              }
            ],
            "help": [
              {
                "return": "help"
              }
            ]
          },
          "default": [
            {
              "return": "other"
            }
          ]
        }
      }
    ]
  }
}
```