Returning a Function Pointer from a Function in C/C++
Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc.
The following is a basic example of a function pointer definition and a function call.
Example
#include <stdio.h>
void funct(int x){
printf(“Value of x is %d\n”,x);
}
int main(){
/* funct_ptr is a pointer to the function funct() */
void (*funct_ptr)(int) = &funct;
/* Calling funct() using funct_ptr pointer */
(*funct_ptr)(99);
return 0;
}
Output:

Take a look at the statement "void (*funct ptr)(int)." If we remove the parentheses surrounding "*funct ptr," we get "void *fun ptr(int)," which is a function declaration that returns a void pointer.
A function pointer, unlike a conventional pointer, points to code rather than data. A function pointer typically holds the beginning of executable code. Unlike conventional pointers, function pointers do not allocate or de-allocate memory.
The name of a function can be used to obtain the address of a function.
Program Code:
Consider the following example: Here, the "&" address operator is removed from the assignment.
We also altered the function call by eliminating the "*," but the code still works.
#include<stdio.h>
void funct(int x){
printf(“Value of x is %d\n”,x);
}
int main(){
/* “&” operator removed */
void (*funct_ptr)(int) = funct;
/* “*” removed */
funct_ptr(99);
return 0;
}
Output:

An array (collection) of function pointers can be used in the same way that conventional pointers are.
In place of the switch case, a function pointer might be used.
Example: In the following application, for example, the user is given an option between zero and two to complete several tasks.
#include <stdio.h>
void add(int x, int y){
printf(“Sum of %d and %d is %d\n”,x,y,x+y);
}
void sub(int x, int y){
printf(“Difference of %d and %d is %d\n”,x,y,x-y);
}
void multiplication(int x, int y){
printf(“Multiplication of %d and %d is %d\n”,x,y,x*y);
}
int main(){
/* funct_ptr_arry is an array (collection) of function pointers */
void (*funct_ptr_arry[])(int, int) = {add, sub, multiplication};
unsigned int abc, x = 10, y = 20;
printf("Enter Choice: 0 for addition, 1 for subtraction and 2 for
multiplication\n");
scanf("%d", &abc);
if (abc > 2) return 0;
(*funct_ptr_arry[abc])(x, y);
return 0;
}
Output:

A function pointer, just like a normal data pointer, can be provided with an argument and returned from a function.
Example: Look at the following C program example, in which wrapper() takes a void funct() argument and calls the provided function.
#include <stdio.h>
void funct1(){
printf(“Function1\n”);
}
void funct2(){
printf(“Function2\n”);
}
void wrapper(void (*funct)()){
funct();
}
int main(){
wrapper(funct1);
wrapper(funct2);
return 0;
}
Output:

This specific idea is quite useful in C. To reduce code duplication, we can utilize function pointers in C. In the case of an array of structures, for example, a simple qsort() method could be used to sort arrays (collections) in any order, ascending or descending. Furthermore, with function pointers and void pointers, the qsort() method can be used for any data type.
Example
#include <stdio.h>
#include <stdlib.h>
int compare (const void *x, const void *y){
return ( *(int*)x - *(int*)y);
}
int main(){
int arry[] = {12, 5, 61, 57, 100, 78};
int m = sizeof(arry)/sizeof(arry[0]), n;
qsort (arry, m, sizeof(int), compare);
for (n=0; n<m; n++){
printf ("%d ", arry[n]);
}
return 0;
}
Output:

We may develop our own methods, similar to qsort(), that can be applied to any data type and can perform several jobs without code duplication.
Program Code:
A search function for just any data type is shown below. In fact, by developing a customized comparison function, we can utilize this search method to identify nearby components (below a threshold).
#include <stdio.h>
#include <stdbool.h>
bool comparison (const void * x, const void * y){
return ( *(int*)x == *(int*)y );
}
int search(void *arry, int arry_size, int element_size, void *m,
bool comparison (const void * , const void *)){
char *ptr = (char *)arry;
int a;
for (a=0; a<arry_size; a++){
if (comparison(ptr + a*element_size, m)){
return a;
}
}
/* If element is not found */
return -1;
}
int main(){
int arry[] = {1, 3, 50, 44, 98, 101};
int n = sizeof(arry)/sizeof(arry[0]);
int m = 44;
printf ("Returned index is %d ", search(arry, n, sizeof(int), &m, comparison));
return 0;
}
Output:

In C++, many object-oriented features are implemented via function pointers available in C.