0

We had a homework assignment which is over now and the professor gave us the solutions. The problem was just to come up with a situation where we could use function pointers and the code it.

His example is this:

#include <iostream>
using namespace std;

int dvalue(int x)
{
  return x*x;
}
int tvalue(int x)
{
  return x*x*x;
}
void printValue(int (*fptr)(int), int x)
{
  cout << fptr(x) << endl;
}

int main()
{
int (*pfnc)(int);
int value = 10;
pfnc = dvalue;
printValue(pfnc, 10);
}

My questions are, is that the same thing as doing this:

int main()
{
cout << devalue(10) << endl;
}

And why do we use them? Do they use less memory? Is it just so we can call more than one function to a parameter or another function to make it simpler? It seems that they are more complicated, at least for a beginning programmer.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Jeremie
  • 45
  • 2

2 Answers2

1

My questions are, is that the same thing as doing this: [...]

In a nutshell, yes.

And why do we use them?

In fact, we don't use them too often, especially in C++. We do use them somewhat more frequently in C.

Nonetheless, it is useful to understand function pointers. Some examples of classical uses are:

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • So it's not something necessary, just a good thing to know for the future? – Jeremie Feb 09 '14 at 15:30
  • 1
    As your understanding of class design, object-orientation and abstraction improves, you are more likely to look toward such concepts. – mungflesh Feb 09 '14 at 15:32
  • 1
    @user3289902: Sometimes they *are* necessary, just not very often. I would consider them a pretty advanced feature of C++ and would not teach them early on. On the other hand, I would expect anyone who claims proficiency in C++ to have a good grasp of function pointers. – NPE Feb 09 '14 at 15:33
1

Why use function pointer?

There are several cases where pointers to function can be of use. One of the most common is the case where you are writing a function to perform a task (such as sorting an array), but you want the user to be able to define how a particular part of that task will be performed (such as whether the array is sorted in ascending or descending order). Let’s take a closer look at this problem as applied specifically to sorting, as an example that can be generalized to other similar problems.

All sorting algorithms work on a similar concept: the sorting algorithm walks through a bunch of numbers, does comparisons on pairs of numbers, and reorders the numbers based on the results of those comparisons. Consequently, by varying the comparison (which can be a function), we can change the way the function sorts without affecting the rest of the sorting code.

Although its used often in sorting, it has other uses such as allowing callbacks when loading from a resource like a database.

More succinctly, you have the benefit of being able to dynamically reference previously defined functions on the fly by simply pointing to them, and of course calling them.

REFERENCE:

http://www.learncpp.com/cpp-tutorial/78-function-pointers/

Community
  • 1
  • 1
jrd1
  • 10,358
  • 4
  • 34
  • 51