38

Possible Duplicate:
How do you pass a function as a parameter in C?

Suppose I have a function called

void funct2(int a) {

}


void funct(int a, (void)(*funct2)(int a)) {

 ;


}

what is the proper way to call this function? What do I need to setup to get it to work?

Mark
  • 8,408
  • 15
  • 57
  • 81
  • 1
    You have two functions, which one are you having problems calling? Note that you have hidden the function `funct2` in `funct` by having an identically named pointer-to-function parameter so you will have to fully qualify `funct2` to call it directly from inside `funct`. – CB Bailey Jun 14 '11 at 06:50
  • This question might help: http://stackoverflow.com/questions/9410/how-do-you-pass-a-function-as-a-parameter-in-c – Jim Jun 14 '11 at 06:46
  • 7
    Right on, because C++ is C as we all know. Welcome to CloseOverflow. In case one uses C++11 there is good read here: http://stackoverflow.com/questions/16111285/how-to-pass-and-execute-anonymous-function-as-parameter-in-c11 -- avoiding passing functions with pointers improves readability. – greenoldman Jun 03 '14 at 12:44
  • A more complete set of answers: http://stackoverflow.com/questions/16111285/how-to-pass-and-execute-anonymous-function-as-parameter-in-c11 – Eponymous Mar 09 '17 at 18:39
  • 7
    How is this a duplicate of the referenced question about C? It is possible that C++ provides (or at some point will provide) a different approach. – pooya13 Jan 14 '19 at 21:01
  • 1
    @pooya13 It's not a duplicate. There are other ways to pass functions as parameters in C++ that are not possible in C (using [functors](https://www.quantstart.com/articles/Function-Objects-Functors-in-C-Part-1), for example). – Anderson Green Nov 22 '19 at 17:32

4 Answers4

37

Normally, for readability's sake, you use a typedef to define the custom type like so:

typedef void (* vFunctionCall)(int args);

when defining this typedef you want the returning argument type for the function prototypes you'll be pointing to, to lead the typedef identifier (in this case the void type) and the prototype arguments to follow it (in this case "int args").

When using this typedef as an argument for another function, you would define your function like so (this typedef can be used almost exactly like any other object type):

void funct(int a, vFunctionCall funct2) { ... }

and then used like a normal function, like so:

funct2(a);

So an entire code example would look like this:

typedef void (* vFunctionCall)(int args);

void funct(int a, vFunctionCall funct2)
{
   funct2(a);
}

void otherFunct(int a)
{
   printf("%i", a);
}

int main()
{
   funct(2, (vFunctionCall)otherFunct);
   return 0;
}

and would print out:

2
Matt Razza
  • 3,524
  • 2
  • 25
  • 29
32

Another way to do it is using the functional library.

std::function<output (input)>

Here reads an example, where we would use funct2 inside funct:

#include <functional>
#include <iostream>

void displayMessage(int a) {
    std::cout << "Hello, your number is: " << a << '\n';
}

void printNumber(int a, std::function<void (int)> func) {
    func(a);
}

int main() {
    printNumber(3, displayMessage);
    return 0;
}

output :

Hello, your number is: 3
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Marine Galantin
  • 1,634
  • 1
  • 17
  • 28
0

A more elaborate and general (not abstract) example

#include <iostream>
#include <functional>
int Add(int a, int b)
{
    return a + b;
}
int Mul(int a, int b)
{
    return a * b;
}
int Power(int a, int b)
{
    while (b > 1)
    {
        a = a * a;
        b -= 1;
    }
    return a;
}
int Calculator(std::function<int(int, int)> foo, int a, int b)
{
    return foo(a, b);
}
int main()
{
    cout << std::endl
         << "Sum: " << Calculator(Add, 1, 2);
    cout << std::endl
         << "Mul: " << Calculator(Mul, 1, 2);
    cout << std::endl
         << "Power: " << Calculator(Power, 5, 2);
    return 0;
}
Michał Leon
  • 2,108
  • 1
  • 15
  • 15
Pawan Kumar
  • 114
  • 9
  • I feel like this doesn't add enough value compared to [this answer](https://stackoverflow.com/a/63982640/11107541) to warrant being a separate answer. – starball Sep 02 '22 at 18:57
-3

check this

typedef void (*funct2)(int a);

void f(int a)
{
    print("some ...\n");
}

void dummy(int a, funct2 a)
{
     a(1);
}

void someOtherMehtod
{
    callback a = f;
    dummy(a)
}
Tatvamasi
  • 2,537
  • 19
  • 14