# The Go Language – Exploring Functions – part 6 – 6.2 – discussing Variadic Functions

Exploring Go - Functions in Go - Variadic Functions - code demonstration

Hi All,

Greetings for the day !!!

We are continuing exploring Go language

In following previous articles we explored introduction to Go, wrote our first Go program, went through basic data types in Go, exploring strings in Go, exploring Constants and started exploring functions.

• Demonstration / sample code for Variadic functions
• Use case for Variadic Functions

• Function is variadic function if it is called with varying number of arguments
• Variadic functions accepts a variable number of arguments
• Example – we have fmt.Println function (fmt package)
• fmt.Println() requires any number of arguments as shown in below fig
• As in below fig, we are using two variants of fmt.Println() – one is with one argument and second call is with two arguments
• Declaration of Variadic functions – To declare Variadic function we need last parameter preceded by an ellipsis, “…” – this means function may get called with any number of arguments
• Example :Lets have a function whose last parameter is variadic as in below code, as in below code – we have param2 is variadic

``````func Variadic_functions(param1 int, param2 ...int){
}``````

``````//main package
package main

//importing - required packages
import (
"fmt" // package for println()
) //import

//main func - Go program entry
func main() {

} //main func

//function have simple one variadic argument
//while calling, we will pass multiple arguments

``````

Output of the program will be

``````Variadic  function demonstration
[HELLO]
[WE RICH]
[LIFE IS BEAUTIFUL]``````

• Variadic functions converts variadic argument to a slice of the type of variable parameter
• This means we can travel also through the range of values of variadic parameter

``````//main package
package main

//importing - required packages
import (
"fmt" // package for println()
) //import

//main func - Go program entry
func main() {

} //main func

//function have simple one variadic argument
//while calling, we will pass multiple arguments

//function have simple one variadic argument
//while calling, we will pass multiple arguments and will traverse through those number of arguments passed

for _, str := range variadicParam {
fmt.Println(str)
} //for

``````

Output of the above program will be

``````Variadic  function demonstration
HELLO
WE
RICH
LIFE
IS
BEAUTIFUL``````

• Variadic functions are mostly used for strings formatting
• Variadic functions are also used in scenario where we don’t know the number of parameters in advance

Thanks for reading!!! Please feel free to discuss in case any questions / suggestions / thoughts !!!

HAVE A GREAT TIME AHEAD !!! LIFE IS BEAUTIFUL 🙂