Function Pointer and callback function in C plus plus programming language

Function Pointer and callback function in C plus plus programming language

Instructor-svgAl-Mamun Sarkar
Apr 03 , 2020

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 

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram