Google News
logo
Golang - Interview Questions
Variadic Functions in Golang.
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 slice 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("FREE", "LEARN"))
fmt.Println(joinstr("Free", "Time", "Learn"))
fmt.Println(joinstr("F", "R", "E", "E"))
	
}
​

Output : 

FREE-LEARN
Free-Time-Learn
F-R-E-E


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{"free", "TIME", "learn"}
fmt.Println(joinstr(element...))
}​

Output : 

free-TIME-learn
Advertisement