title: C++: newton raphson and overloading functions
body: <p>I wrote a simple implementation of the newton raphson root finding algorithm which takes an initial guess <code>init</code>, a unary function <code>f</code> and the tolerance <code>tol</code> as arguments, as shown below:</p>
  
  <pre><code>bool newton_raphson(double&amp; init,
                      double(*f)(double),
                      double tol){
      const int max_iter = 10000;
      double next_x, soln = init;
      int i = 0;
  
      while(++i &lt; max_iter){
          next_x = soln - f(soln)/fp_x(f, soln);
          if(fabs(next_x - soln) &lt; tol){
              init = next_x;
              return true;
          }
          soln = next_x;
      }
      return false;
  }
  
  double fp_x(double(*f)(double),
              double x){
      const double h = 0.000001;
      return (f(x + h) - f(x - h))/2.0/h;
  }
  </code></pre>
  
  <p>My question is: although this works perfectly fine for unary functions, I would like to change the implementation so that it works for functions <code>f</code> that have more than one parameter, but all except one parameter have constant values. To clarify: if I have a function f(x) = 3x + 2 as shown below</p>
  
  <pre><code>double f(double x){
      return (3*x + 2);
  }
  </code></pre>
  
  <p>Then my implementation works. However, I would also like it to work for any functions with any given number of arguments, but only the first argument is variable. So, if I have a function f(x,y) = 3x + 2y</p>
  
  <pre><code>double f(double x, double y){
      return (3*x + 2*y);
  }
  </code></pre>
  
  <p>I would like to find the root of f(x,2), or f(x,3) using the same function, and so on for n arguments, not just one or two (please ignore the idea that the functions I showed in the example are simple linear functions, this is just an example). Is there any way to implement the function for a varying number of arguments or do I have to write an implementation for every case?</p>
  
  <p>Thanks,</p>
  
  <p>NAX</p>
  
  <p><strong>NOTE</strong></p>
  
  <p>As you could tell by now, this question isn't really about newton-raphson, but it makes it easier if I use it as an example for the actual question, which is a single implementation for functions of different numbers of arguments.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>A few answers below use <code>std::bind</code> and <code>std::function</code> to solve the problem, which actually better address my question than the selected answer; however, they are c++11 library classes/functions, (which, don't get me wrong, is something I strongly urge every c++ programmer to go ahead and learn) and at the time of this writing, I was facing some problems using them; Eclipse Juno using g++ 4.7 (which is c++11 compliant) still somehow failed to recognize <code>std::function</code>, and I therefore decided to go and stick with the checked answer below, which also works nicely.</p>