Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Passing Variable Number of Arguments into Functions | Arrays and Slices
Introduction to GoLang

bookPassing Variable Number of Arguments into Functions

In the previous section, we explored functions; however, we didn't delve into a specific type of function called variadic functions. These are functions that can accept a variable number of arguments. Now that we have some understanding of arrays, let's take a closer look at variadic functions.

Variadic functions are a special type of function that can accept a variable number of arguments. The syntax for declaring a variadic function is as follows:

To define a variadic function, we use the ... syntax followed by the variable's type. For example:

func myFunction(args ...int) {
    // Function body
}

In this example, myFunction is a variadic function that can accept any number of integer arguments. The term args in this context represents an array containing all the arguments passed to the function, making it effectively an array of type int.

For instance, we can create a function called sum that accepts a variable number of int numbers and returns the sum of those numbers:

index.go

index.go

copy
12345678910111213141516
package main import "fmt" func sum(args ...int) int { var total int = 0 for i := 0; i < len(args); i++ { total += args[i] } return total } func main() { fmt.Println(sum(1, 2, 3)) // Output: 6 fmt.Println(sum(2, 5, 7, 9, 12)) // Output: 35 fmt.Println(sum(47, 100, 121, 50)) // Output: 318 }

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 7

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 1.96

bookPassing Variable Number of Arguments into Functions

Swipe to show menu

In the previous section, we explored functions; however, we didn't delve into a specific type of function called variadic functions. These are functions that can accept a variable number of arguments. Now that we have some understanding of arrays, let's take a closer look at variadic functions.

Variadic functions are a special type of function that can accept a variable number of arguments. The syntax for declaring a variadic function is as follows:

To define a variadic function, we use the ... syntax followed by the variable's type. For example:

func myFunction(args ...int) {
    // Function body
}

In this example, myFunction is a variadic function that can accept any number of integer arguments. The term args in this context represents an array containing all the arguments passed to the function, making it effectively an array of type int.

For instance, we can create a function called sum that accepts a variable number of int numbers and returns the sum of those numbers:

index.go

index.go

copy
12345678910111213141516
package main import "fmt" func sum(args ...int) int { var total int = 0 for i := 0; i < len(args); i++ { total += args[i] } return total } func main() { fmt.Println(sum(1, 2, 3)) // Output: 6 fmt.Println(sum(2, 5, 7, 9, 12)) // Output: 35 fmt.Println(sum(47, 100, 121, 50)) // Output: 318 }

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 7
some-alt