<p>I want to create a function that performs a function passed by parameter on a set of data. How do you pass a function as a parameter in C?</p>

[{'answer_id': 9411, 'author': 'saint_groceon', 'author_id': 59, 'author_profile': 'https://Stackoverflow.com/users/59', 'pm_score': 2, 'selected': False, 'text': '<p>You need to pass a <a href="http://www.newty.de/fpt/fpt.html#passPtr" rel="nofollow noreferrer">function pointer</a>.  The syntax is a little cumbersome, but it\'s really powerful once you get familiar with it.</p>\n'}, {'answer_id': 9413, 'author': 'Niyaz', 'author_id': 184, 'author_profile': 'https://Stackoverflow.com/users/184', 'pm_score': 11, 'selected': True, 'text': '<p><strong>Declaration</strong></p>\n\n<p>A prototype for a function which takes a function parameter looks like the following:</p>\n\n<pre><code>void func ( void (*f)(int) );\n</code></pre>\n\n<p>This states that the parameter <code>f</code> will be a pointer to a function which has a <code>void</code> return type and which takes a single <code>int</code> parameter. The following function (<code>print</code>) is an example of a function which could be passed to <code>func</code> as a parameter because it is the proper type:</p>\n\n<pre><code>void print ( int x ) {\n  printf("%d\\n", x);\n}\n</code></pre>\n\n<p><strong>Function Call</strong></p>\n\n<p>When calling a function with a function parameter, the value passed must be a pointer to a function. Use the function\'s name (without parentheses) for this:</p>\n\n<pre><code>func(print);\n</code></pre>\n\n<p>would call <code>func</code>, passing the print function to it.</p>\n\n<p><strong>Function Body</strong></p>\n\n<p>As with any parameter, <code>func</code> can now use the parameter\'s name in the function body to access the value of the parameter. Let\'s say that <code>func</code> will apply the function it is passed to the numbers 0-4. Consider, first, what the loop would look like to call print directly:</p>\n\n<pre><code>for ( int ctr = 0 ; ctr &lt; 5 ; ctr++ ) {\n  print(ctr);\n}\n</code></pre>\n\n<p>Since <code>func</code>\'s parameter declaration says that <code>f</code> is the name for a pointer to the desired function, we recall first that if <code>f</code> is a pointer then <code>*f</code> is the thing that <code>f</code> points to (i.e. the function <code>print</code> in this case). As a result, just replace every occurrence of print in the loop above with <code>*f</code>:</p>\n\n<pre><code>void func ( void (*f)(int) ) {\n  for ( int ctr = 0 ; ctr &lt; 5 ; ctr++ ) {\n    (*f)(ctr);\n  }\n}\n</code></pre>\n\n<p><a href="http://math.hws.edu/bridgeman/courses/331/f05/handouts/c-c++-notes.html" rel="noreferrer">Source</a></p>\n'}, {'answer_id': 9421, 'author': 'roo', 'author_id': 716, 'author_profile': 'https://Stackoverflow.com/users/716', 'pm_score': 7, 'selected': False, 'text': '<p>This question already has the answer for defining function pointers, however they can get very messy, especially if you are going to be passing them around your application. To avoid this unpleasantness I would recommend that you typedef the function pointer into something more readable. For example.</p>\n\n<pre><code>typedef void (*functiontype)();\n</code></pre>\n\n<p>Declares a function that returns void and takes no arguments. To create a function pointer to this type you can now do:</p>\n\n<pre><code>void dosomething() { }\n\nfunctiontype func = &amp;dosomething;\nfunc();\n</code></pre>\n\n<p>For a function that returns an int and takes a char you would do</p>\n\n<pre><code>typedef int (*functiontype2)(char);\n</code></pre>\n\n<p>and to use it</p>\n\n<pre><code>int dosomethingwithchar(char a) { return 1; }\n\nfunctiontype2 func2 = &amp;dosomethingwithchar\nint result = func2(\'a\');\n</code></pre>\n\n<p>There are libraries that can help with turning function pointers into nice readable types. The <a href="http://www.boost.org/doc/libs/1_36_0/doc/html/function.html" rel="noreferrer">boost function</a> library is great and is well worth the effort!</p>\n\n<pre><code>boost::function&lt;int (char a)&gt; functiontype2;\n</code></pre>\n\n<p>is so much nicer than the above.</p>\n'}, {'answer_id': 31521586, 'author': 'Richard', 'author_id': 752843, 'author_profile': 'https://Stackoverflow.com/users/752843', 'pm_score': 7, 'selected': False, 'text': '<p>Since C++11 you can use the <a href="http://www.cplusplus.com/reference/functional/function/function/" rel="noreferrer">functional library</a> to do this in a succinct and generic fashion. The syntax is, e.g.,</p>\n\n<pre><code>std::function&lt;bool (int)&gt;\n</code></pre>\n\n<p>where <code>bool</code> is the return type here of a one-argument function whose first argument is of type <code>int</code>.</p>\n\n<p>I have included an example program below:</p>\n\n<pre><code>// g++ test.cpp --std=c++11\n#include &lt;functional&gt;\n\ndouble Combiner(double a, double b, std::function&lt;double (double,double)&gt; func){\n  return func(a,b);\n}\n\ndouble Add(double a, double b){\n  return a+b;\n}\n\ndouble Mult(double a, double b){\n  return a*b;\n}\n\nint main(){\n  Combiner(12,13,Add);\n  Combiner(12,13,Mult);\n}\n</code></pre>\n\n<p>Sometimes, though, it is more convenient to use a template function:</p>\n\n<pre><code>// g++ test.cpp --std=c++11\n\ntemplate&lt;class T&gt;\ndouble Combiner(double a, double b, T func){\n  return func(a,b);\n}\n\ndouble Add(double a, double b){\n  return a+b;\n}\n\ndouble Mult(double a, double b){\n  return a*b;\n}\n\nint main(){\n  Combiner(12,13,Add);\n  Combiner(12,13,Mult);\n}\n</code></pre>\n'}, {'answer_id': 33870738, 'author': 'Yogeesh H T', 'author_id': 3725702, 'author_profile': 'https://Stackoverflow.com/users/3725702', 'pm_score': 5, 'selected': False, 'text': '<p>Pass <strong>address of a function as parameter to another function</strong> as shown below</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\nvoid print();\nvoid execute(void());\n\nint main()\n{\n    execute(print); // sends address of print\n    return 0;\n}\n\nvoid print()\n{\n    printf("Hello!");\n}\n\nvoid execute(void f()) // receive address of print\n{\n    f();\n}\n</code></pre>\n\n<p>Also we can pass function as parameter using <strong>function pointer</strong></p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\nvoid print();\nvoid execute(void (*f)());\n\nint main()\n{\n    execute(&amp;print); // sends address of print\n    return 0;\n}\n\nvoid print()\n{\n    printf("Hello!");\n}\n\nvoid execute(void (*f)()) // receive address of print\n{\n    f();\n}\n</code></pre>\n'}, {'answer_id': 53503192, 'author': 'doppelheathen', 'author_id': 4206613, 'author_profile': 'https://Stackoverflow.com/users/4206613', 'pm_score': 4, 'selected': False, 'text': '<p>Functions can be "passed" as function pointers, as per ISO C11 6.7.6.3p8: "<em>A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’</em>, as in 6.3.2.1. ".  For example, this:</p>\n\n<pre><code>void foo(int bar(int, int));\n</code></pre>\n\n<p>is equivalent to this:</p>\n\n<pre><code>void foo(int (*bar)(int, int));\n</code></pre>\n'}, {'answer_id': 55174838, 'author': 'Walter', 'author_id': 7270598, 'author_profile': 'https://Stackoverflow.com/users/7270598', 'pm_score': -1, 'selected': False, 'text': '<p>It\'s not really a function, but it is an localised piece of code. Of course it doesn\'t pass the code just the result.  It won\'t work if passed to an event dispatcher to be run at a later time (as the result is calculated now and not when the event occurs). But it does localise your code into one place if that is all you are trying to do.</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\nint IncMultInt(int a, int b)\n{\n    a++;\n    return a * b;\n}\n\nint main(int argc, char *argv[])\n\n{\n    int a = 5;\n    int b = 7;\n\n    printf("%d * %d = %d\\n", a, b, IncMultInt(a, b));\n\n    b = 9;\n\n    // Create some local code with it\'s own local variable\n    printf("%d * %d = %d\\n", a, b,  ( { int _a = a+1; _a * b; } ) );\n\n    return 0;\n}\n</code></pre>\n'}, {'answer_id': 65977096, 'author': 'Hemanth Kollipara', 'author_id': 11534375, 'author_profile': 'https://Stackoverflow.com/users/11534375', 'pm_score': 2, 'selected': False, 'text': '<p>I am gonna explain with a simple example code which takes a <code>compare</code> function as parameter to another <code>sorting</code> function.\nLets say I have a bubble sort function that takes a custom compare function and uses it instead of a fixed if statement.</p>\n<p>Compare Function</p>\n<pre><code>bool compare(int a, int b) {\n    return a &gt; b;\n}\n</code></pre>\n<p>Now , the Bubble sort that takes another function as its parameter to perform comparison</p>\n<p>Bubble sort function</p>\n<pre><code>void bubble_sort(int arr[], int n, bool (&amp;cmp)(int a, int b)) {\n\n    for (int i = 0;i &lt; n - 1;i++) {\n        for (int j = 0;j &lt; (n - 1 - i);j++) {\n            \n            if (cmp(arr[j], arr[j + 1])) {\n                swap(arr[j], arr[j + 1]);\n            }\n        }\n    }\n}\n</code></pre>\n<p>Finally , the <code>main</code> which calls the Bubble sort function by passing the boolean compare function as argument.</p>\n<pre><code>int main()\n{\n    int i, n = 10, key = 11;\n    int arr[10] = { 20, 22, 18, 8, 12, 3, 6, 12, 11, 15 };\n\n    bubble_sort(arr, n, compare);\n    cout&lt;&lt;&quot;Sorted Order&quot;&lt;&lt;endl;\n    for (int i = 0;i &lt; n;i++) {\n        cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;\n    }\n}\n</code></pre>\n<p>Output:</p>\n<pre><code>Sorted Order\n3 6 8 11 12 12 15 18 20 22\n</code></pre>\n'}, {'answer_id': 69301994, 'author': 'mada', 'author_id': 16972547, 'author_profile': 'https://Stackoverflow.com/users/16972547', 'pm_score': -1, 'selected': False, 'text': '<pre><code>typedef int function();\n\nfunction *g(function *f)\n{\n    f();\n    return f;\n}\n\nint main(void)\n{\n    function f;\n\n    function *fn = g(f);\n\n    fn();\n}\n\nint f() { return 0; }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/9410', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/432/']