> ## 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.

# Kotlin Vararg Parameter

A `vararg` (variable number of arguments) parameter allows a function to accept zero or more arguments of a specified type. Declared using the `vararg` modifier, it instructs the Kotlin compiler to pack the comma-separated arguments provided at the call site into an array accessible within the function body.

## Syntax and Type Mapping

When you declare a `vararg` parameter of type `T`, the compiler translates it into an `Array<out T>` within the function scope.

```kotlin theme={"dark"}
fun processStrings(vararg elements: String) {
    // 'elements' is treated as Array<out String>
    println(elements.size)
}

processStrings("Alpha", "Beta", "Gamma")
processStrings() // Valid: results in an empty array
```

To prevent the performance overhead of boxing and unboxing, Kotlin optimizes `vararg` parameters for primitive types. A `vararg` of a primitive type compiles directly to its corresponding specialized primitive array (e.g., `IntArray`, `DoubleArray`, `CharArray`).

```kotlin theme={"dark"}
fun processIntegers(vararg numbers: Int) {
    // 'numbers' is treated as IntArray, not Array<Int>
}
```

## The Spread Operator (`*`)

If you already have an array and want to pass its contents to a function expecting a `vararg` parameter, you must unpack the array using the spread operator (`*`). Passing the array directly will result in a type mismatch, as the function expects individual elements, not the array reference itself.

```kotlin theme={"dark"}
val stringArray = arrayOf("Delta", "Echo")

// Unpacking the array elements into the vararg parameter
processStrings(*stringArray) 

// The spread operator can be combined with discrete arguments
processStrings("Alpha", *stringArray, "Foxtrot")
```

*Note: The spread operator only works with arrays. To pass a `List` or other `Collection`, it must first be converted using `.toTypedArray()`.*

```kotlin theme={"dark"}
val stringList = listOf("Golf", "Hotel")
processStrings(*stringList.toTypedArray())
```

## Parameter Positioning and Resolution Rules

Kotlin enforces specific rules regarding the placement of a `vararg` parameter within a function signature:

1. **Single Vararg Limit:** A function can declare a maximum of one `vararg` parameter.
2. **Trailing Parameters:** While it is conventional to place the `vararg` parameter at the end of the parameter list, it is not strictly required. However, if parameters follow a `vararg`, they must be passed using named arguments at the call site to resolve ambiguity.

```kotlin theme={"dark"}
fun configure(vararg flags: String, isEnabled: Boolean) {
    // Implementation
}

// 'isEnabled' must be explicitly named
configure("DEBUG", "VERBOSE", isEnabled = true)
```

If a function signature includes a functional parameter (lambda) following a `vararg`, trailing lambda syntax can be used without explicitly naming the parameter.

```kotlin theme={"dark"}
fun execute(vararg commands: String, action: () -> Unit) {
    // Implementation
}

execute("START", "RUN") {
    println("Executing action")
}
```

<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 Kotlin 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>
