Golang program to create a function with the variable number of arguments to calculate the sum of all arguments
belongs to collection: Golang Variadic Function Programs
All Answers
total answers (1)
belongs to collection: Golang Variadic Function Programs
total answers (1)
Program/Source Code:
The source code to create a function with the variable number of arguments to calculate the sum of all arguments is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package to formatting related functions.
In this program, we created a variadic function calculateSum() to accept variable number of arguments.
In the main() function, we called the calculateSum() function with different number of arguments. The calculateSum() function returns the sum of given arguments to the calling function. Then we printed the result on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer