When would you use Variadic Functions in a real project
Fast recall
Quick revision
Core answer
Practical understanding of Variadic Functions.
Repeating a definition without saying when to use it.
Give one trade-off and say how you would test it.
Interview-ready guide
Interview answer
Executed both sides:
func sum(nums ...int) int { /* sum the slice */ }
nums := []int{1, 2, 3}
sum(nums...) // == sum(1, 2, 3) — the slice expands into args
sum(4, 5) // direct call — no slice neededThe spread and the direct call produce identical callee state — a []int containing the elements. The spread is the bridge when your data is already a slice: logWithPrefix(msg, parts...), append(dst, src...).
When would you use Variadic Functions in a real project
Direct Answer
slice... at the call site expands the slice's elements as individual arguments. sum(slice...) == sum(slice[0], slice[1], ...). The elements are passed — the callee still receives them as a (new) slice.
Deep Dive & Architecture
The two uses of the SAME symbol, which is the confusion to clear: in a signature, ...T means 'collect these arguments into a slice.' At a call site, s... means 'expand this slice into arguments.' Declaration collects; call site expands. Every built-in that takes lists works this way — append(slice, items...) is the canonical example: append's second parameter is variadic, and the idiom appends a whole slice via spread.
The immutability/copy subtlety worth one sentence in an interview: the variadic slice you receive is a NEW slice (the runtime allocates and copies the arguments — except in the spread-of-existing-slice optimization, where the runtime MAY pass the original — which is why modifying a variadic parameter is a style bug: the caller may or may not see the changes).
Common Mistakes & Pitfalls
Repeating a definition without saying when to use it.
Check your understanding
Answer these without looking back at the article.
- What is Variadic Functions, and when would you use it?
- 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.