Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could be the expression "double (f)(double)" in C mean

Tags:

c

Like I already wrote in the title I have the problem that I need to understand what double (f)(double) could mean in C. The whole methode header looks like this:

Bmp* drawGraph(double (f)(double),double minX,double maxX)

It's for a university project an my professor likes to be absent or not reachable through email or other ways of communication.

I think the name and so the propose of the methode is pretty much self-explaining.

In the letter of information to this method was said that "f" should be a function but I don't know what kind of parameter I should give the method in that case.

Bmp* drawGraph(double (f)(double),double minX,double maxX)
{
    double height = f(maxX);

    Bmp* bmp = newBmp(maxX, f(maxX) * 2);

    background(bmp, BLACK);

    //Hier zeichne ich das Koordinatensystem
    //in seiner minimalistischten Form

    drawLine(bmp, GREEN, 0, f(maxX), maxX, f(maxX));
    drawLine(bmp, GREEN, 0, 0, 0, f(maxX) * 2);

    for(double d = minX; d < maxX; d += 0.1)
    {
            drawLine(bmp, RED, d, f(d) + height, d + 0.1, f(d + 0.1) + height);
    }

    return bmp;
}
like image 299
Akkyen Avatar asked Jul 30 '14 11:07

Akkyen


1 Answers

double (f)(double)

f is a parameter of function type: a function that has a double parameter and that returns a double value.

For example:

double foo(double a)
{
    return a + 42.0;
}

The parameter is adjusted (and therefore the declaration is equivalent) to a pointer to a function that has a double parameter and that returns a double value.

double (*f)(double)

So these declarations are all equivalent:

void bla(double (f)(double));
void bla(double f(double));
void bla(double (*f)(double));
like image 79
ouah Avatar answered Nov 11 '22 04:11

ouah