C Programming Language: Passing a Function as a Parameter

C Programming Language: Passing a Function as a Parameter

After reading this title, you?re probably feeling like this:

Image for post

This may be a little hard to understand as first. I mean, a function parameter? You probably have some questions, such as:

  • The fuck is that!?
  • How does it works?
  • When should I use it?
  • Is it really worth it?

Fear no more! I shall answer all these questions, and more! 😉

Declaration

First, how do I declare this? A simple prototype for a function which takes a function parameter (sometimes called a formal parameter), is something like this:

void myfunction(void (*f)(int));

So? what???

This states that a parameter f will be a pointer (*f) to the function myFunction, which has a void return type and which takes just a single int parameter. In lay man’s terms, myFunction takes an argument of a function type void, that returns a type void, and takes an int as an argument; (void (*f)(int)).

If you?re not following yet, let?s try this.

  • void myFunction(void (*f)(int));

And inside the parameter, we have:

  • void *f(int);

Funtion Call

Try compiling the following program and see if it works.

#include <stdio.h>void printNumber(int nbr) { printf(“%dn”, nbr);}void myFunction(void (*f)(int)) { for(int i = 0; i < 5; i++) { (*f)(i); }}int main(void) { myFunction(printNumber); return (0);}

If this was a tad hard to understand, maybe try this one.

#include <stdio.h>void print() { printf(“Hello World!”);}void helloworld(void (*f)()) { f();}int main(void) { helloworld(print); return (0);}

It?s recommended to create functions that can work with a variety of parameter types. You can do this by declaring the parameter to by of type void *, and then casting the appropriate type in the function body, i.e.: void (*f)(int).

Another Example

Now that we?re pros with functions parameter, let?s try doing some functions that may be helpful for you.

void striter(char *s, void(*f)(char *));

This function applies the function f to each character of the string passed as argument. Each character is passed by address to f to be modified if necessary. We can build this function in this way:

void striter(char *s, void (*f)(char *)) { if (s) { for (int i = 0; s[i]; i++) { f(&s[i]); } } return ;}

  • if(s) makes sure the function is protected (so when it get’s a null pointer, it does nothing.
  • for (int i = 0; s[i]; i++) begins the counter to sweep by each character of the string.
  • f(&s[i]); takes the address of each character of the string, and pass it into the function f.

Completed Code

#include <stdio.h>void printNumber(int nbr) { printf(“%dn”, nbr);}void myFunction(void (*f)(int)) { for(int i = 0; i < 5; i++) { (*f)(i); }}int main(void) { myFunction(printNumber); return (0);}#include <stdio.h>void print() { printf(“Hello World!”);}void helloworld(void (*f)()) { f();}int main(void) { helloworld(print); return (0);}void striter(char *s, void (*f)(char *)) { if (s) { for (int i = 0; s[i]; i++) { f(&s[i]); } } return ;}

Sources

  • Niyaz? answer to How do you pass a function as a parameter in C?
  • http://math.hws.edu/bridgeman/courses/331/f05/handouts/c-c++-notes.html

No Responses

Write a response