3

What's the real benefit of using pointers to functions instead of those functions itself? Does it make execution faster ? Or does it provide convenience when it's passed to another function?

Naveen
  • 74,600
  • 47
  • 176
  • 233
Ghost
  • 1,777
  • 6
  • 31
  • 44
  • 1
    are you using `C` or `C++`? Since you added tag `visual-c++` I added `C++` tag. – Naveen May 15 '12 at 12:12
  • @Griwes I'm just asking when pointers to functions can be advantageous – Ghost May 15 '12 at 12:20
  • @Ghost, "using pointers to functions instead of those functions itself" implies that you don't know the basic difference between them - you "use functions" at compile time and pointers to them at runtime. There is no "advantage" or "disadvantage" - they are used **in different situations**, so comparing them is this way doesn't make sense. – Griwes May 15 '12 at 12:29
  • @Griwes: I think you're being a bit harsh here. What Ghost is essentially asking is in what circumstances would you might use function pointers. That's a reasonable question. In C++, of course, we have run-time polymorphism which implies that virtual functions can be decided at runtime, thus your statement *you "use functions" at compile time* is not always true. Sure, function pointers are an invaluable facility, but occasionally they are misused or over used, when a polymorphic approach would have worked fine. – Component 10 May 15 '12 at 13:10
  • @Griwes Well if it was in my book, dyou think i'd ASK it again over here? – Ghost May 15 '12 at 14:45
  • @Ghost, then get better book. – Griwes May 15 '12 at 15:01

5 Answers5

6

It enables you to "select" a component of your business logic around at run time -- contrast this with hardcoding the function names, which limits you to choosing which function to use at compile time.

Jon
  • 428,835
  • 81
  • 738
  • 806
3

There are situations where you must use a pointer to function, there is no other way. For example, to implement a callback function, specifying comparator function(the last parameter in sorting routines).

EDIT: As specified in the comment, this is about C. Function pointer came from C.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • Couldn't you use a function reference in C++? I have limited experience with C++ so I don't know if it's possible. – onemasse May 15 '12 at 12:14
  • Sorry, I am also not sure about C++ function reference. Function pointer came from C and in C there are no alternative in these cases. – taskinoor May 15 '12 at 12:17
  • Of course, but the question is tagged C++. And I know that in C++ you often want to use references instead of pointers. If you don't know I don't expect you to answer. But maybe someone else could. – onemasse May 15 '12 at 12:20
  • @onemasse, OP is asking about pointers to function and my answer provides info about where they might be useful. I doubt that OP was asking about the difference between pointer and reference. Accepted answer and comment in question also shows that OP was looking for this kind of information. Can you please explain more what you meant by function reference? – taskinoor May 15 '12 at 12:25
  • 1
    @onemasse: No, you can't use a function reference. References are bound at the time of the first creation of the value and cannot be unbound. Callbacks are generally things you want to be able to *change*, or to simply not register at all (and therefore be NULL, which references also can't be). – Nicol Bolas May 15 '12 at 12:26
  • Sorry, I interpreted the question as the OP wanted to know why you used function pointers in C++. – onemasse May 15 '12 at 12:27
2

Function pointers are how you store functions in variables and pass them into other functions. There are no "advantages" over "regular functions", they are completely different things and trying to compare them doesn't make sense. It's like asking "which is better, variables or if statements?"

Function pointers provide a functionality that would otherwise be unavailable.

user229044
  • 232,980
  • 40
  • 330
  • 338
0

In fact passing a pointer of a function is a little bit slower than calling the function itself. But the difference is so little that it can hardly ever have any effect.

As Jon said, it brings more flexibility in some cases, when you can pass the function from one part of your programm to another.

superM
  • 8,605
  • 8
  • 42
  • 51
  • I don't see any reason why it should be slower to call a function pointer instead of a regular function. That must be highly dependent on the compiler. – onemasse May 15 '12 at 12:24
0

function pointers are used in many situations where you are not sure which function to call exactly. Lets take an example. you want to sort an array. For that you want to write a generic sort function . Now this sorting function needs to compare the array elements. But since array can have any elements (int, floats, strings, user defined types etc), how this sort function can compare the elements. So, in this case, you can pass the ordering-determining function as an argument to this sort function and based on that it can sort the array. Also, it is usefull in another way as well. Lets say you want to sort an array of string (a string of numbers). you may want to sort it numerrically or in alphabatically.

In this case, when you want to sort nnumerically, you can pass compare function which compares based on the value of string converted to int. and so on...