# Function Pointers.1

## Introduction To Function Pointers

### What are Function Pointers?

• Function pointers  are pointers – Variables – which point to the address of a function.
• Actually Function pointers provide some extremely interesting, efficient and elegant programming techniques. You can use them to replace Switch-Statements or to Implement Callbacks.
• A Function Pointer always points to function with a specific signature  , so all function this pointer will point to must have the same parameters and return type.

### Introductory Example:

How To Replace Switch-Statement??
1. #include <iostream>
2. using namespace std;
3. //#Definiation and declaration of Function pointers
4. float Plus (float a, float b) { return a+b; }
5. float Minus (float a, float b) { return a-b; }
6. float Multiply(float a, float b) { return a*b; }
7. float Divide (float a, float b) { return a/b; }
8. void Switch(float a, float b, char opCode)
9. {
10.     float result;
11.     switch(opCode)
12.     {
13.     case ‘+’ : result = Plus (a, b); break;
14.     case ‘-‘ : result = Minus (a, b); break;
15.     case ‘*’ : result = Multiply (a, b); break;
16.     case ‘/’ : result = Divide (a, b); break;
17.     }
18.     cout << “Switch: 2+5=” << result << endl;
19. }
20. void Switch_With_Function_Pointer(float a, float b, float (*op)(float, float))
21. {
22.     float result = op(a, b); // call using function pointer
23.     cout << “Switch replaced by function pointer: 2-5=”; // display result
24.     cout << result << endl;
25. }
26. void Replace_A_Switch()
27. {
28.     cout << endl << “Executing function ’Replace_A_Switch’” << endl;
29.     Switch(2, 5, ‘+’);
30.     Switch_With_Function_Pointer(2, 5, Minus);
31. }
32. //#end Definiation and declaration of Function pointers
33. int main()
34. {
35.     Replace_A_Switch();
36.     return 0;
37. }

### The syntax of C++  Function Pointer:

• There are two different types of Function Pointers :
1. Pointers to static C++ member functions.
2. Pointers to non-static C++ member functions.
• The basic diﬀerence is that all pointers to non-static member functions need a hidden argument (“this.”pointer to an instance of the class).

### Define a Function Pointer:

• Function Pointer is nothing else than variable ,so it must be defined as usual.
1. int (MyClass::*ptrMember)(float, char, char) = NULL;
2. int (MyClass::*ptrConstMember)(float, char, char) const = NULL;

### Initializing Function Pointer:

• To initialize function pointer ,you must give it the address of the function in your program
1. #include <iostream>
2. using namespace std;
3. void coutFunction(int x)
4. {
5.     printf( “%d\n”, x );
6. }
7. int main()
8. {
9.     void (*COUT)(int);
10.     // the ampersand is optional
11.     COUT = &coutFunction;
12.     return 0;
13. }

### Using Function Pointer:

• To call the function pointed to by a function pointer, you treat the function pointer as though it was the name of the function you wish to call. The act of calling it performs dereferencing operation,  there’s no need to do it yourself:
1. /* call coutFunction(note that you do not need to write (*COUT)(2) ) */
2. COUT( 2 );
3. /* but if you want to, you may */
4. (*COUT)( 2 );

### Generic Sorting Routine:

• If you were to write a sort routine, you might want to allow the function’s caller to choose the order in which the data is sorted; some might need to sort the data in ascending order, others might prefer descending order . The much nicer way of allowing the user to choose how to sort the data is simply to let the user pass in a function to the sort function. This function might take two pieces of data and perform a comparison on them.
1. #include <iostream>
2. using namespace std;
3. int int_sorter( const void *first_arg, const void *second_arg )
4. {
5.     int first = *(int*)first_arg;
6.     int second = *(int*)second_arg;
7.     if ( first < second )
8.     {
9.         return -1;
10.     }
11.     else if ( first == second )
12.     {
13.         return 0;
14.     }
15.     else
16.     {
17.         return 1;
18.     }
19. }
20. int main()
21. {
22.     int array[10];
23.     int i;
24.     /* fill array */
25.     for ( i = 0; i < 10; ++i )
26.     {
27.         array[ i ] = 10 – i;
28.     }
29.     qsort( array, 10 , sizeof( int ), int_sorter );
30.     for ( i = 0; i < 10; ++i )
31.     {
32.         printf ( “%d\n” ,array[ i ] );
33.     }
34. return 0;
35. }

### Benefits of Function Pointers:

•  Function pointers provide a way of passing around instructions for how to do something.
•  You can write flexible functions and libraries that allow the programmer to choose behavior by passing function pointers as arguments.
• This flexibility can also be achieved by using classes with virtual functions.

## 10 thoughts on “Function Pointers.1”

1. Very good, Anas, I had no much knowledge about “Function pointers” It’s really useful and interesting 🙂
Keep it up man, Waiting more interesting posts …

1. Anas says:

Thanks Amr, I’m really happy to hear this..
I’ll post all parts soon, GOD willing…

1. Anas says:

Thanks dear Sepres … I hope so for you too 🙂

2. It’s interesting that OOP is strongly related to this; an object’s virtual table is a collection of function pointers 🙂

1. Anas says:

Thanks Zikas for this link …

3. Thanks Anas for this useful topic, it’s really interesting
keep it up

1. Anas says:

Your are welcome dear Morsy ..
This is really encouraging for me