Declaring a function of fewer variables within a function that takes a few of the variables as arguments

I have a numerical integrator (using RK4), that takes functions of one variable of any type and integrates them. The function that needs to be integrated is, and needs to be, of multiple variables. The issue then, is how to use this function as an argument to the integrator without the use of globals.

In pseudo-code:
1
2
3
4
5
6
7
8
g(x,a,b) {
  ...
}

f(a,b,c,d,n) {
  g(x) = g(x,a,b); <<-- How do I do this?
  return integral(g(x),c,d,n);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// default values in functions
#include <iostream>
using namespace std;

int divide (int a, int b=2)
{
  int r;
  r=a/b;
  return (r);
}

int main ()
{
  cout << divide (12);
  cout << endl;
  cout << divide (20,4);
  return 0;
}

from http://www.cplusplus.com/doc/tutorial/functions2/
A default value is not sufficient. I need to ether overload g with a one variable function declared in the same namespace as f or declare a new function of one variable (that is identical in content to g) in the namespace of f.
Last edited on
Sounds like you want a functional programming language. They are pretty much made for this kind of stuff (If I'm understanding this correctly).
This is part of a larger project, so changing languages is extremely difficult if not entirely excluded. Incidentally, this is why globals are not an option.
Last edited on
Are you trying to do something like

1
2
auto g = std::bind(g, _1, a, b);
return integral(g, c, d, n);


If so, look into std::bind (or boost::bind for legacy compilers)
That is exactly what I want. Thanks.
Topic archived. No new replies allowed.