Variadic Functions in Go
The function that called with the varying number of arguments is known as variadic function. Or in other words, a user is allowed to pass zero or more arguments in the variadic function. fmt.Printf is the example of the variadic function, it required one fixed argument at the starting after that it can accept any number of arguments.
Important Points:
- In the declaration of the variadic function, the type of the last parameter is preceded by an ellipsis, i.e, (…). It indicates that the function can be called at any number of parameters of this type.
Syntax:
function function_name(para1, para2...type)type{ // code... }
- Inside the function …type behaves like a slice. For example, suppose we have a function signature, i.e, add( b…int)int, now the a parameter of type[]int.
- You can also pass an existing slice in a variadic function. To do this, we pass a slice of the complete array to the function as shown in Example 2 below.
- When you do not pass any argument in the variadic function, then the silce inside the function is nil.
- The variadic functions are generally used for string formatting.
- You can also pass multiple slice in the variadic function.
- You can not use variadic parameter as a return value, but you can return it as a slice.
Example 1:
// Go program to illustrate the // concept of variadic function package main import( "fmt" "strings" ) // Variadic function to join strings func joinstr(element...string)string{ return strings.Join(element, "-" ) } func main() { // zero argument fmt.Println(joinstr()) // multiple arguments fmt.Println(joinstr( "GEEK" , "GFG" )) fmt.Println(joinstr( "Geeks" , "for" , "Geeks" )) fmt.Println(joinstr( "G" , "E" , "E" , "k" , "S" )) } |
chevron_right
filter_none
Output:
GEEK-GFG Geeks-for-Geeks G-E-E-k-S
Example 2:
// Go program to illustrate the // concept of variadic function package main import( "fmt" "strings" ) // Variadic function to join strings func joinstr(element...string)string{ return strings.Join(element, "-" ) } func main() { // pass a slice in variadic function element:= []string{ "geeks" , "FOR" , "geeks" } fmt.Println(joinstr(element...)) } |
chevron_right
filter_none
Output:
geeks-FOR-geeks
When we use a Variadic function:
- Variadic function is used when you want to pass a slice in a function.
- Variadic function is used when we don’t know the quantity of parameters.
- When you use variadic function in your program, it increase the readability of your program.