Variadic Functions
Fast recall
Quick revision
Core answer
Whether you understand Variadic Functions beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
The variadic parameter collects arguments into a slice:
func sum(nums ...int) int {
total := 0
for _, n := range nums { total += n }
return total
}
sum(1, 2, 3) // 6 — three separate args, collected into []int{1,2,3}
sum() // 0 — zero args, empty slice
sum([]int{4, 5}...) // 9 — spread an existing sliceExecuted. Inside the function, nums IS a []int — the same loop, indexing, len, slicing as any slice.
The rules: exactly ONE variadic parameter, and it must be the LAST. func f(a string, nums ...int) is legal (fixed args first); func f(nums ...int, a string) is a compile error.
The spread operator (...) — the same symbol, different position: in the declaration it means 'collect'; at the call site after a slice it means 'expand.' The pair composes: variadic collection + spread = passing slices through.
Variadic Functions
Direct Answer
func sum(nums ...int) — the function receives nums as a SLICE ([]int). Call with any count: sum(1,2,3), sum(), sum(slice...) (spread a slice). The ... is the variadic marker, on the parameter and on the spread.
Deep Dive & Architecture
What it's for: printf-shaped APIs (fmt.Println takes ...interface{} — any count of anything), option patterns, builders. What it's NOT: the only way to pass slices — the spread form means a slice-based call is always available.
The interface{} variadic: func log(args ...interface{}) — any count of any type. fmt's whole API is built on this — and so is every print-shaped helper you've written.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Variadic Functions in a real project?
- How would you compare Variadic Functions with an alternative?
- What is a common mistake when using Variadic Functions?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.