function type vs. function pointer type

c++

Solution

Somewhat confusingly, you can declare a function to take a function as a parameter (even though that makes no sense), and the effect is to make the parameter a function pointer. This is similar to the way you can declare a function parameter that looks like an array, but is actually a pointer.

The function argument can be the name of the function, with or without a `&` to explicitly take its address. If you omit the `&`, then there's an implicit function-to-pointer conversion. Again, this is similar to passing a (pointer to) an array, where the implicit array-to-pointer conversion means you only need to write the array's name, rather than `&array[0]`.

That rule doesn't apply when declaring variables; `int goo(int);` (with or without unnecessary parentheses around `goo`) declares a function, not a pointer, and you can't assign to functions.

Problem

I'm trying to understand the difference between the following two blocks of code: ``` void f(int (*func)(int)) { func(5); } ``` and ``` void g(int (func)(int)) { func(5); } ``` Both functions work in the same way given the following code: ``` int blah(int a) { cout << "hello" << endl; return 0; } int main() { f(blah); g(blah); return 0; } ``` However, if I write the following code: ``` int (*foo)(int); int (goo)(int); foo = blah; goo = blah; ``` I get a compile error for goo = blah. But in the first example, I could call make the function call g(blah) which appears to be quite similar to goo = blah. Why does one work and not the other?

Original source