I have a function my_func()
, which takes 2 parameters a
and b
.
I want to define a function inside the solve_for_b_by_bisection()
function, called f
, such that I can just call f(b)
, which is my_func(a, b)
for some fixed input a
. How do I do that? Do I use a function pointer?
The reason I am doing this instead of calling f(a,b)
directly is that in the actual thing I am working on, it has 10+ variables which are constants - it is not possible to repeat the variable list every time.
double my_func(const double a, const double b)
{
/* some arbitary function */
}
double solve_for_b_for_contant_a_by_bisection (const double a,
const double upperbound,
const double lowerbound)
{
double (*my_func_pointer)(const double b)
{
&my_func(a, b)
}
lowerboundvalue = *my_func(lowerbound)
upperboundvalue = *my_func(upperbound)
midpointvalue = *my_func(0.5 * (lowerbound+upperbound))
/* The rest of the implementation */
}
You might use lambda:
auto func = [a](double b) { return my_func(a, b); };