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

Exploring Go - Functions in Go - Variadic Functions - code demonstration
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.

In this article we will continue to explore functions – we will discuss “Variadic functions

Take away from this article

  • What are Variadic Functions
  • Demonstration / sample code for Variadic functions
  • How Variadic Function works
  • Use case for Variadic Functions

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
Exploring Go - Functions in Go - Variadic Functions
fig : Exploring Go – Functions in Go – Variadic Functions
  • 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){
     //Variadic functions implementation goes here
}

//main package
package main

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

//main func - Go program entry
func main() {
	fmt.Println("Variadic  function demonstration")

	//Calling Variadic_function_ReturnStrings with 1 argument
	Variadic_function_ReturnStrings("HELLO")
	//Calling Variadic_function_ReturnStrings with 2 arguments
	Variadic_function_ReturnStrings("WE", "RICH")
	//Calling Variadic_function_ReturnStrings with 3 arguments
	Variadic_function_ReturnStrings("LIFE", "IS", "BEAUTIFUL")

} //main func

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

func Variadic_function_ReturnStrings(variadicParam ...string) {
	//Variadic functions implementation goes here
	fmt.Println(variadicParam)
} //Variadic_function_JoinStrings

Output of the program will be

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

Exploring Go - Functions in Go - Variadic Functions - code demonstration
fig : Exploring Go – Functions in Go – Variadic Functions – code demonstration
  • How variadic function works
    • 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() {
	fmt.Println("Variadic  function demonstration")

	//Calling Variadic_function_ReturnStrings with 1 argument
	//Variadic_function_ReturnStrings("HELLO")
	//Calling Variadic_function_ReturnStrings with 2 arguments
	//Variadic_function_ReturnStrings("WE", "RICH")
	//Calling Variadic_function_ReturnStrings with 3 arguments
	//Variadic_function_ReturnStrings("LIFE", "IS", "BEAUTIFUL")

	//Calling Variadic_function_TravelStrings with 1 argument
	Variadic_function_TravelStrings("HELLO")
	//Calling Variadic_function_TravelStrings with 2 arguments
	Variadic_function_TravelStrings("WE", "RICH")
	//Calling Variadic_function_TravelStrings with 3 arguments
	Variadic_function_TravelStrings("LIFE", "IS", "BEAUTIFUL")

} //main func

//simple variadic function demo
//function have simple one variadic argument
//while calling, we will pass multiple arguments
func Variadic_function_ReturnStrings(variadicParam ...string) {
	//Variadic functions implementation goes here
	fmt.Println(variadicParam)
} //Variadic_function_ReturnStrings

//simple variadic function demo
//function have simple one variadic argument
//while calling, we will pass multiple arguments and will traverse through those number of arguments passed
func Variadic_function_TravelStrings(variadicParam ...string) {
	//Variadic functions implementation goes here

	//looping throgh the variadic parameter
	for _, str := range variadicParam {
		fmt.Println(str)
	} //for

} //Variadic_function_TravelStrings

Output of the above program will be

Variadic  function demonstration
HELLO
WE
RICH
LIFE
IS
BEAUTIFUL

Exploring Go - Functions in Go - Variadic Functions - code demonstration - traversing through Variadic parameter
fig : Exploring Go – Functions in Go – Variadic Functions – code demonstration – traversing through Variadic parameter
  • 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 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: