C Why use a pointer as a parameter instead of a function?

I read about functions with functions as parameters, and especially in C, they use function pointers. Suppose I want to implement the newton raphson method (in a simple way) to calculate 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 calculate the approximation for the derivative, I need a function that returns double and takes double as an argument. The same goes for calculating the root of a function, given other parameters. My question is: why is this different from declaring function parameters as function pointers? For example, declaring the input parameter f as a function pointer, not just a function ...

+2
source share
2 answers

The f parameter is a pointer to a function in both derivative and newton_raphson .

 double derivative(double f(double), double x) { ... } 

exactly equivalent

 double derivative(double (*f)(double), double x) { ... } 

Only, the first one looks better - usually when you can skip parentheses, you probably should do this. In the end, both are equivalent.

 double ((((derivative)))(double (((*(f))))(double ((trouble))), double ((x)))) { ... } 

I hope that it will be used only in IOCCC.


However, if you declare by defining a variable (not a function parameter), you need to use

 double (*f)(double); 

as

 double f(double); 

is just a function declaration.


6.7.6.3 The declaration of functions (including prototypes) of the C11 draft n1570 project states:

Declaring a parameter as a function '' return type should be adjusted to a "pointer" to the return function type, as in 6.3.2.1.

A 6.9.1 Function definitions further say that

[...] the type of each parameter is configured as described in 6.7.6.3 for the list of parameter types; the resulting type must be a full object type.

Additionally has the following example:

EXAMPLE 2

To pass one function to another, we can say that

 int f(void); /* ... */ g(f); 

Then the definition of g can read

 void g(int (*funcp)(void)) { /* ... * (*funcp)(); /* or funcp(); ... */ } 

or, equivalently,

 void g(int func(void)) { /* ... */ func(); /* or (*func)(); ... */ } 
+7
source

Like regular data pointers, a function pointer can be passed as an argument and can also be returned from a function. The function name contains the address of the function.

My question is: why is this different from declaring function parameters as function pointers? For example, declaring the input parameter f as a function pointer, not just a function ...

The answer is that both forms will be processed by the compiler the same way. But to read your code, go to the type of declaration that your example code has, i.e.

 double derivative(double f(double), double x) { ... } 

Even in C, the function definitions given below will be interpreted as the same -

 void foo(int a[]) // or int a[10] { ... } void foo(int *a) { ... } 
0
source

Source: https://habr.com/ru/post/1258588/


All Articles