> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Go Return Statement

The `return` statement in Go terminates the execution of the current function and transfers control back to the calling function. If the function's signature specifies return types, the `return` statement must provide expressions that evaluate to values matching the function signature's declared return types.

## Syntax

```go theme={"dark"}
return [expressionList]
```

## Structural Mechanics

**1. Terminating Statements**
Functions declared without a return type (void functions) do not require a `return` statement at the end of their block. An empty `return` can be used to halt execution prematurely.

Functions declaring one or more return types must end with a *terminating statement*. While an explicit `return` is the most common way to satisfy this, Go's compiler accepts any valid terminating statement. This includes a call to a built-in terminating function (like `panic`), an infinite loop (`for {}`), a `goto` statement, or a `switch`/`select` block where all possible execution paths end in a terminating statement.

```go theme={"dark"}
package main

import "fmt"

// Implicit return at the end of the block
func logMessage(msg string) {
    fmt.Println(msg)
}

// Explicit empty return to halt execution
func haltEarly(condition bool) {
    if condition {
        return 
    }
    fmt.Println("Continued")
}

func main() {
    logMessage("System starting...")
    haltEarly(true)  // Will return immediately
    haltEarly(false) // Will print "Continued"
}
```

**2. Multiple Return Values**
Go natively supports multiple return values. The `expressionList` typically contains comma-separated expressions that map positionally to the types defined in the function signature. Alternatively, Go allows a single expression in a multiple-return statement if that expression is a call to a multi-valued function whose return types perfectly match the caller's return signature.

```go theme={"dark"}
package main

import "fmt"

// Returns comma-separated expressions
func getCoordinates() (int, int) {
    return 10, 20 
}

// Returns a single expression that yields multiple values
func forwardCoordinates() (int, int) {
    return getCoordinates() 
}

func main() {
    x, y := forwardCoordinates()
    fmt.Printf("X: %d, Y: %d\n", x, y)
}
```

**3. Named Return Values and Naked Returns**
If a function signature assigns identifiers to its return values, Go treats them as variables defined at the top of the function block, initialized to their respective zero values. A `return` statement without an `expressionList` in such a function is called a **naked return**. It implicitly yields the current state of those named variables.

```go theme={"dark"}
package main

import "fmt"

func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    return // Implicitly returns the current values of x and y
}

func main() {
    a, b := split(18)
    fmt.Printf("Split: %d, %d\n", a, b)
}
```

*Note: Even with named return values, you can still provide an explicit `expressionList` to override the named variables at the point of return.*

## Execution Order and `defer`

The `return` statement is not an atomic operation. When a `return` statement is encountered, Go executes the return sequence in a specific, deterministic order:

1. **Evaluation:** The expressions in the `expressionList` are evaluated.
2. **Assignment:** The evaluated results are assigned to the function's return variables (either named or implicit).
3. **Deferred Execution:** Any functions scheduled via the `defer` keyword are executed in Last-In-First-Out (LIFO) order.
4. **Yield:** Control is officially transferred back to the caller.

Because deferred functions execute *after* the return variables are assigned but *before* control is yielded, a deferred closure can mutate named return variables.

```go theme={"dark"}
package main

import "fmt"

func increment() (i int) {
    defer func() { i++ }()
    return 1 // 1 is assigned to i, then defer increments i to 2. The caller receives 2.
}

func main() {
    result := increment()
    fmt.Printf("Result: %d\n", result)
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Go Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
