Michael Sazonov
Michael Sazonov

Reputation: 1533

Function Arguments in C

I'm totally new to C. Here's the question:

Write the function

fzero(double f(double),double x1, double x2)

as we did in class and use it to find all the solutions of

sin( pi*x / (1+x^2) ) = 0.25.

Now, I don't want you to solve the this. I'd missed this lecture and only want to understand what means

double f(double);

Upvotes: 2

Views: 148

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272717

In that context, it means that f is a function pointer to a function to that takes one double argument, and returns a double.

As an example:

void foo(double f(double))
{
    double y = f(3.0);  // Call function through function pointer
    printf("Output = %f\n", y);   // Prints "Output = 9.0000"
}

double square(double x)
{
    return x*x;
}

int main(void)
{
    foo(&square);  // Pass the address of square()
}

Note that there are two syntaxes for function pointers:

void foo(double f(double))
void foo(double (*f)(double))

These are equivalent.

Upvotes: 9

Related Questions