Function Pointer and callback function in C plus plus programming language

Author: Al-mamun Sarkar Date: 2020-04-03 17:38:42

Function Pointer and callback function in C plus plus programming language. The following code shows how to use function pointer as callback function in C plus plus Programming Language.

 

Code:

#include
using namespace std;

int ascending(int a, int b)
{
    if(a > b)
        return 1;
    else
        return -1;
}


int descending(int a, int b)
{
    if(a > b)
        return -1;
    else
        return 1;
}



void bubbleSort(int *data, int n, int (*compare)(int, int))
{
    int temp;
    for(int i=0; i 0 ) {
                temp = data[i];
                data[i] = data[j];
                data[j] = temp;
            }
        }
    }

    // Show output
    for(int i=0; i

 

Output:


Ascending Order : 
1 2 3 4 5 6 7 8 9 

Descending Order : 
9 8 7 6 5 4 3 2 1