How to pass function pointer as parameter in C?

Write a C program to pass function pointer as parameter to another function. How to pass functions as parameter to another function in C programming.

Functions are powerful in dividing code segments to different modules. They are easy to maintain. Every program contains two components data and behaviour. Variables wraps data whereas functions wraps behaviour.

We know how to pass data/variables as parameter to function. In this post I will explain how to pass behaviour i.e. function itself as a parameter to another function.

Required knowledge

Functions, Pointers, Function Pointers

How to pass function pointer as parameter

Logically speaking you cannot pass a function to another function. However, this does not mean that the above descriptions are misleading.

In C programming you can only pass variables as parameter to function. You cannot pass function to another function as parameter. But, you can pass function reference to another function using function pointers. Using function pointer you can store reference of a function and can pass it to another function as normal pointer variable. And finally in the function you can call function pointer as normal functions.

Let us see how to declare, initialize and use function pointer to access a function using pointers. We will use this function pointer to pass functions as arguments to another function.

  1. Declare a function pointer with function prototype it can point. Let us declare a function pointer that can point to functions returning void and accepts no parameter.
    void (*greet)();
  2. Initialize function pointer by storing reference of a function.
    void greetMorning()
    {
         printf("Good, morning!");
    }
    
    greet = greetMorning;
    
  3. Finally invoke (call) the function using function pointer.
    greet();

Let us combine this all together and write program to pass function pointers as parameter to another function.

Program to pass function pointer as parameter

/**
 * C program to pass function pointer as parameter to another function
 */

#include <stdio.h>


/* Function declarations */
void greetMorning();
void greeEvening();
void greetNight();

void greet(void (*greeter)());



int main()
{
    // Pass pointer to greetMorning function 
    greet(greetMorning);


    // Pass pointer to greetEvening function 
    greet(greeEvening);


    // Pass pointer to greetNight function 
    greet(greetNight);


    return 0;
}


/**
 * Function to print greeting message.
 * 
 * @greeter     Function pointer that can point to functions
 *              with no return type and no parameters.
 */
void greet(void (*greeter)())
{
    // Call greeter function pointer
    greeter();
}



void greetMorning() 
{
    printf("Good, morning!\n");
}


void greeEvening() 
{
    printf("Good, evening!\n");
}


void greetNight() 
{
    printf("Good, night!\n");
}

Output

Good, morning!
Good, evening!
Good, night!

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>