> For the complete documentation index, see [llms.txt](https://muhammad-tri-wibowo.gitbook.io/java-tutorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://muhammad-tri-wibowo.gitbook.io/java-tutorials/basic-java/variadic-function.md).

# Variadic Function

In Java, variadic functions allow you to pass a variable number of arguments to a method. Unlike regular methods, which have a fixed number of parameters, variadic functions can accept a varying number of arguments. This flexibility is achieved using an ellipsis (`...`) in the method parameter list. Here's how variadic functions work in Java:

#### Syntax for Variadic Function:

```java
return_type methodName(data_type... variableName) {
    // Method implementation
}
```

* `return_type`: Specifies the data type the method returns.
* `methodName`: Specifies the name of the variadic function.
* `data_type... variableName`: Indicates a variable number of parameters of the specified data type.
* Inside the method, you can treat `variableName` as an array of the specified data type.

#### Example of Variadic Function:

```java
public class VariadicFunctionExample {

    // Variadic function to calculate the sum of integers
    public static int calculateSum(int... numbers) {
        int sum = 0;
        for (int num : numbers) {
            sum += num;
        }
        return sum;
    }

    public static void main(String[] args) {
        int sum1 = calculateSum(1, 2, 3, 4, 5);
        int sum2 = calculateSum(10, 20, 30);

        System.out.println("Sum 1: " + sum1);
        System.out.println("Sum 2: " + sum2);
    }
}
```

**Explanation:**

1. **`public static int calculateSum(int... numbers) { ... }`:** This line defines a variadic function named `calculateSum`. It accepts a variable number of integers (`numbers`) as arguments.
2. **`for (int num : numbers) { sum += num; }`:** In the method body, the function iterates over the `numbers` array (which is treated as an array due to the variadic parameter) and calculates the sum of all integers passed to the function.
3. **`int sum1 = calculateSum(1, 2, 3, 4, 5);`:** This line calls the `calculateSum` function with five integers as arguments and assigns the returned sum to the variable `sum1`.
4. **`int sum2 = calculateSum(10, 20, 30);`:** This line calls the `calculateSum` function with three integers as arguments and assigns the returned sum to the variable `sum2`.
5. **`System.out.println("Sum 1: " + sum1);`:** Prints the sum of the first set of integers.
6. **`System.out.println("Sum 2: " + sum2);`:** Prints the sum of the second set of integers.

When you run this Java program, it will output:

```mathematica
Sum 1: 15
Sum 2: 60
```

This output demonstrates the usage of variadic functions to handle different numbers of arguments, providing flexibility in method calls.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://muhammad-tri-wibowo.gitbook.io/java-tutorials/basic-java/variadic-function.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
