Golang program that uses func with variable argument list Last Updated : 29 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Golang, a function that can be called with a variable argument list is known as a variadic function. One can pass zero or more arguments in the variadic function. If the last parameter of a function definition is prefixed by ellipsis ..., then the function can accept any number of arguments for that parameter. Syntax of a variadic function: func f(elem ...Type) Here ... operator tells Golang program to store all arguments of Type in elem parameter. After that, Go create an elem variable of the type []Type. Therefore, all the values passed are stored in an elem parameter which is a slice. A slice can also be passed in the argument instead of the argument list, as finally function is converting them into a slice. For more information you can refer to Variadic Functions in Golang Advantages of using a Variadic Function: Passing a slice in a function is very easy.Useful when the number of parameters is unknown.Increases the readability of the program.Let's see some of the examples to use functions with variable argument list: Example 1: Go // Go program that uses a function // with variable argument list package main // Importing required packages import ( "fmt"; ) // Variadic function to return // the sum of the numbers func add(num ...int) int { sum := 0 for _, j := range num { sum += j } return sum } func main() { fmt.Println("Sum =", add(1, 2, 3, 4, 5, 7, 8, 6, 5, 4)) } [tabbyending] Output: Sum = 45 Example 2: A slice can also be used as an argument list. Go// Go program that uses a function // with variable argument list // Using a slice as the argument list package main // importing required modules import ( "fmt" ) // Function to check if an element // is present in the list or not func check(x int, v ...int) { flag := false index := 0 for i, j := range v { if j == x { flag = true index = i } } if flag { fmt.Println("Element ", x, " found at index:", index) } else { fmt.Println("Element not present in the list") } } func main() { el := []int{1, 1, 2, 3, 4, 5, 6, 7, 8, 9} check(1, el...) check(10, el...) }[tabbyending] Output: Element 1 found at index: 1 Element not present in the list Comment More infoAdvertise with us Next Article Golang program that uses func with two arguments H Hritika Rajput Follow Improve Article Tags : Go Language Go-Functions Golang-Program Similar Reads Golang program that uses func with two arguments Functions may use certain parameters to receive values and perform the task required accordingly. The syntax for a function with two arguments is: func function_name( [parameter_1 parameter_2] ) [return_types] { body of the function } Example 1: Without any return type C // Golang program that uses 1 min read Golang Program that uses func as Local Variable Function performs a specific task, section of code that defined once can be reused. Functions are used to make your code easier to understand by breaking it into small and understandable tasks.Functions are also known as method, sub-routine, or procedure. General form of a function definition in Go 3 min read Different Ways to Find the Type of Variable in Golang Variable is a placeholder of the information which can be changed at runtime. And variables allow to Retrieve and Manipulate the stored information. There are 3 ways to find the type of variables in Golang as follows: Using reflect.TypeOf Function Using reflect.ValueOf.Kind() Function Using %T with 2 min read Golang Program that Uses Named Return Values and Defaults Golang functions have special functionality that allows them to provide the name to the return values. These named return values can be used as arguments or variables. The named return values also use the default values for the data types like 0 for int type etc. To understand this concept let's tak 1 min read Golang Program that Uses Multiple Return Values In Golang, we can return multiple values at a time from a single function. Multiple return values can be achieved by changing the return type of the function in the function signature. Syntax :Â func value( ) ( int , int ) {Â return 1 , 0 ;Â }Â The (int, int) in this function signature explains that 2 min read reflect.MethodByName() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.MethodByName() Function in Golang is used to get function value corresponding to the method of v with the given n 2 min read Like