Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Returning a function pointer from a function in C/C++

  • Difficulty Level : Expert
  • Last Updated : 09 Sep, 2021

In C/ C++, like normal data pointers(int *, char *, etc), there can be pointers to functions. Every function created in a program gets an address in memory since pointers can be used in C/C++, so a pointer to a function can also be created.

Syntax:

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

return type (*function_pointer_name) (argument_type_1,  argument_type_2, ……, argument_type_n) = &function_name;

OR



return type (*function_pointer_name) (argument_type_1, argument_type_2, ……, argument_type_n) = function_name;

NOTE: Arguments type and return type of function pointer should match with the actual function present in the program.

Program 1:

C




// C program for the above approach
#include <stdio.h>
 
// Function to add the value 10 to
// the variable a
void demo(int* a) { *a += 10; }
 
// Driver Code
int main()
{
    int num = 20;
 
    // ptr contains address of demo
    // function or void
    void (*ptr)(int*) = &demo;
 
    // or (*ptr)(&num);
    ptr(&num);
 
    printf("%d", num);
 
    return 0;
}


C++




// C++ program for the above approach
#include <iostream>
using namespace std;
 
void demo(int& a)
{
    a += 10;
}
 
// Driver Code
int main()
{
    int num = 20;
 
    // Now ptr contains address of demo
    // function or void
    void (*ptr)(int*) = &demo;
 
    // or (*ptr)(num);
    ptr(num);
 
    cout << num << endl;
 
    return 0;
}


 
 

Output

30

 

Return Function Pointer From Function: To return a function pointer from a function, the return type of function should be a pointer to another function. But the compiler doesn’t accept such a return type for a function, so we need to define a type that represents that particular function pointer.

 



Syntax :

 

typedef return type (*function_pointer_name) (argument_type_1, argument_type_2, ……, argument_type_n);

This creates a type which represents a pointer for a particular function.

 

Program 2:

 

C




// C program for the above approach
#include <stdio.h>
typedef int (*ptr)(int*);
typedef ptr (*pm)();
 
int fun1(int* y)
{
    printf("geeks!!\n");
    return *y + 10;
}
 
// Function that return type ptr
ptr fun()
{
    printf("Hello ");
 
    // or return fun1;
    /* or
     int(*pt)(int*)=fun1;
     return pt
  */
    return &fun1;
}
 
// Driver Code
int main()
{
    int a = 10;
 
    pm u = fun;
 
    printf("%d", (*u())(&a));
 
    return 0;
}


C++




// C++ program for the above approach
#include <iostream>
using namespace std;
typedef int (*ptr)(int*);
typedef ptr (*pm)();
 
int fun1(int* y)
{
    cout << "geeks!!" << endl;
    return *y + 10;
}
 
// Function that returns the type ptr
ptr fun()
{
    cout << "Hello ";
    return &fun1;
}
 
// Driver Code
int main()
{
    int a = 10;
    pm u = fun;
    cout << (*u())(&a) << endl;
 
    return 0;
}


 
 

Output

Hello geeks!!
20

 



Declaring an array that has two function pointers as to its elements and these function pointers, in turn, return other function pointers which point to other functions. The logic of the driver code main() function can be changed in the above program as:

 

Program 3:

 

C




// C program for the above approach
#include <stdio.h>
 
// This defines a type for
// function prototypes
typedef int (*ptr)(int*);
typedef ptr (*pm)();
 
int fun1(int* y)
{
    printf("geeks!!\n");
    return *y + 10;
}
 
// fun() is a function with
// return type ptr
ptr fun()
{
    printf("Hello ");
 
    // or return fun1;
    /* or
     int(*pt)(int*)=fun1;
     return pt
  */
    return &fun1;
}
 
// Driver code
int main()
{
    int a = 10;
    pm u = fun;
 
    /*
    Above line assigns 'u' which is
    of type 'pm' to an array of size
    1 which has function pointers as
    its elements and these function
    pointers in turn return other
    function pointer which points to
    other functions.
 
    Now this 'p' array contains a function
    pointer 'u' which points to fun() and
    this fun() returns another function
    pointer which points to fun1().
  */
    int (*(*p[1])())(int*) = { u };
 
    printf("%d", (*p[0]())(&a));
}


C++




// C++ program for the above approach
#include <iostream>
using namespace std;
 
// This defines a type for
// function prototypes
typedef void (*ptr)(int&);
typedef ptr (*pm)();
 
void fun1(int& z)
{
    printf("geeks!!\n");
    cout << z + 10 << endl;
}
 
// Function that returns type ptr
ptr fun()
{
    printf("Hello ");
 
    // or return fun1;
    /* or
     int(*pt)(int*)=fun1;
     return pt
  */
    return &fun1;
}
 
// Driver Code
int main()
{
    int a = 10;
    pm u = fun;
 
    /*
    Above line assigns 'u' which is
    of type 'pm' to an array of size
    1 which has function pointers as its
    elements and these function pointers
    in turn return other function pointer
    which points to other functions.
 
    Now this 'p' array contains a function
    pointer 'u' which points to fun() and
    this fun() returns another function
    pointer which points to fun1() and
    this fun1() returns void.
  */
    void (*(*p[1])())(int&) = { u };
 
    (*p[0]())(a);
}


 
 

Output

Hello geeks!!
20

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!