title: C++ : Function overloading vs Variadic function vs Variadic template vs default parameter
body: <p>I have seen these questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/15876590/overload-a-method-or-use-default-values-c">Overload a method or use default values? c++</a></li>
  <li><a href="https://stackoverflow.com/questions/14284952/function-overloading-vs-default-argument-in-c">function overloading vs default argument in c++</a></li>
  </ul>
  
  <p>but they do not consider variadic functions and variadic template . </p>
  
  <p>Consider the problem of writing a function to find the average of either 2 or 3 positive numbers .</p>
  
  <p>There are four ways we could implement this function:</p>
  
  <ol>
  <li><p>Using a default parameter:</p>
  
  <pre><code>int average(int x , int y , int z=-1);
  </code></pre></li>
  <li><p>Using function overloads:</p>
  
  <pre><code>int average(int x , int y);
  int average(int x , int y , int z);
  </code></pre></li>
  <li><p>Using a <a href="http://en.wikipedia.org/wiki/Variadic_function" rel="nofollow noreferrer">variadic function</a></p>
  
  <pre><code>int average(int x,int y, ...);
  </code></pre></li>
  <li><p>Using a <a href="http://en.wikipedia.org/wiki/Variadic_template" rel="nofollow noreferrer">variadic template</a></p>
  
  <pre><code>int sum=0;
  int count=0;
  void average(){
     return;
  }
  template &lt;class A, class ...B&gt; void average(A argHead, B... argTail){
     sum =sum+argHead;
     count+=1;
     average(argTail...);
  }
  </code></pre></li>
  </ol>
  
  <p>What are the pros and cons of using one method over another ?</p>