-2

'compare' is a function and while passing it as a parameter, the function call parenthesis isn't used, still, the function is called. why?

I tried int k = compare; but that threw error. i don't know what's happening!

//compare function:
bool compare(int a, int b){
    return a<b;
}

// ... and in main() :
 li.merge(li1, compare); // where li and li1 are lists.

I hoped it should throw errors like function call isn't defined.

melpomene
  • 84,125
  • 8
  • 85
  • 148

2 Answers2

2

You are passing the funtion as variable, without executing the function (by not doing ())

It will be invoked, but thats because li.merge will call it somewhere, later, in its logic. Its not directly being called when you pass it as argument.

Stefan
  • 17,448
  • 11
  • 60
  • 79
1

You're actually giving merge a pointer to the compare function, and it calls that function by the address you gave it whenever it needs to do so. Even without knowing how merge is implemented, you can imagine it needs to call compare multiple times. Here's a blurb on function pointers

bitbangs
  • 506
  • 3
  • 6