I have been reading about having functions with functions as parameters, and particulary in C, they use function pointers. Let's suppose I want to implement the newton raphson method (in a simple way) for computing zeros in non linear equations.
double derivative(double f(double), double x)
{
double h = 1e-9;
return (f(x + h) - f(x)) / h;
}
double newton_raphson(double f(double), double x0, double tol)
{
double xk, diff;
do
{
xk = x0 - f(x0) / derivative(f, x0);
diff = fabs(xk - x0);
x0 = xk;
} while (diff >= tol);
return xk;
}
So, to compute an approximation for derivative I need a function that returns a double and takes a double as an argument. Same for computing a root of the function, given the other parameters. My question is, why is this different from declaring function parameters as function pointers? For instance declaring the input parameter f as a function pointer instead of just a function...
The parameter
f
is a pointer-to-function in bothderivative
andnewton_raphson
.is exactly equivalent to
Only, the former looks nicer - usually when you can omit parentheses, you should probably do so. After all both of them are equivalent to
That I hope will only ever be used in IOCCC.
However, if you're declaring, defining a variable (not a function parameter), you need to use
as
is just a function declaration.
6.7.6.3 Function declarators (including prototypes) of C11 draft n1570 says:
And 6.9.1 Function definitions further says that
additionally it has the following example:
Like normal data pointers, a function pointer can be passed as an argument and can also be returned from a function. A function’s name holds the address of function.
The answer is that both forms will be treated as same by compiler. But for readibility of your code, go with the kind of declaration that your example code has, i.e.,
Even in C, the function definitions given below will be interpreted as same-