How do you pass a function as a parameter in C?

Do you search the following questions?

  • how to pass a function as a parameter in C?
  • Can use a function as a parameter of another function?
  • Can a function be a parameter?
  • How do function parameters work?
  • Example code to passing function pointer as an argument.

If your question is one of the above-mentioned questions, then you are at the right place. I will explain with the help of example code how you can pass a function as a parameter in another function.

You must have basic knowledge of pointer and function, these are primary prerequisites of this question. So it is my recommendation that if you are learning C and function pointer you must read the below articles.

 

How to pass function pointer as a parameter?

In C, we can pass parameters two ways call by value and call by reference. We use call by reference to pass a function as an argument in another function.

This facilitates some generic programming in which entire tasks are passed as arguments to a function. This feature also helps to implement call-back. The qsort library function is the best example where we pass compare function as a parameter.

Let’s consider an example for better understanding.

Suppose you want to pass the below function as an argument in another function named is foo().

void printMessage()
{
     printf("Thanks for reading Aticleworld.com\n");
}

 

Now the first step is to declare a prototype for a function that takes the above function as a parameter.

//foo function prototype

void foo( void (*fPtr)() );

 

The above prototype states that the parameter fPtr will be a pointer to a function that has a void return type and which takes no parameter.

Now function printMessage could be passed to foo as a parameter because its prototype is supported by foo  function.

 

I believe the next question that comes to your mind is how to use the function that is passed in the parameter. It means how to use printMessage  in foo .

Don’t worry see the body of the foo function you will understand it automatically.

void foo(void (*fPtr)())
{
    fPtr();
    /*
      if you want you can use 
      it like below expression.
      
      (*fPtr)();
      
    */
}

 

Since fPtr is a pointer to the function and holds the address of the desired function. So using fPtr we can call the function.

Let us combine all things and write a complete program to pass function pointers as parameters to another function.

C program to pass function pointer as parameter:

 

#include<stdio.h>

void print()
{
    printf("Thanks for reading Aticleworld.com\n");
}


void foo(void (*fPtr)())
{
    fPtr();
    /*
      if you want you can use
      it like below expression.

      (*fPtr)();

    */
}

int main()
{
    foo(print);

    return 0;
}

Output:

How do you pass a function as a parameter in C Aticleworld

 

Now you know how to pass a function in another function with the help of the function pointer. The prototype of the caller function becomes messy if you pass the function that takes many arguments.

To prevent this, I recommend that you should typedef the function pointer to something more understandable. For example.

//typedef function pointer

typedef void (*pfCallback)();

 

Now you use the pfCallback in place of a complex prototype, see the below code.

#include<stdio.h>

//typedef function pointer
typedef void (*pfCallback)();


void print()
{
    printf("Thanks for reading Aticleworld.com\n");
}


void foo(pfCallback fPtr)
{
    fPtr();
    /*
      if you want you can use
      it like below expression.

      (*fPtr)();

    */
}

int main()
{
    foo(print);

    return 0;
}

 

 

Recommended Post:

Leave a Reply

Your email address will not be published. Required fields are marked *