Alternative to nested functions in C?

392 views Asked by At

What is the standard alternative to nested functions in C? How do I give one function the scope of another function? I don't really want to pass around extra parameters like a struct, use variable length input, or use the gcc extension unless there is no other way.

For my specific problem, I have this function:

double findroot(double (*fp)(double), double start, double end);

It finds the root of a function, f(x), of one variable with a function pointer as an argument.

I want to pass in a function of multiple variables where all but one are fixed. For example, f(x,a,b,c), where a, b, and c are all constants that are passed in and x is the only variable that changes. How do I find f(0,a,b,c) while keeping the findroot function the same so that the findroot function is reusable and generic and not specific to this one case?

Thanks.

1

There are 1 answers

0
Clifford On

If you need to find f(x,a,b,c) where a, b and c are invariant, then that suggests a new more specialised function f2(x) = f(x,a,b,c). For example:

double f2( double x )
{    
    return f( x, 1.0, 2.0, 3.0 ) ;
}