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?
Declaration
A prototype for a function which takes a function parameter looks like the following:
void func ( void (*f)(int) );
This states that the parameter f will be a pointer to a function which has a void return type and which takes a single int parameter. The following function (print) is an example of a function which could be passed to func as a parameter because it is the proper type:
void print ( int x ) {
printf("%d\n", x);
}
Function Call
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:
func(print);
would call func, passing the print function to it.
Function Body
As with any parameter, func can now use the parameter's name in the function body to access the value of the parameter. Let's say that func will apply the function it is passed to the numbers 0-4. Consider, first, what the loop would look like to call print directly:
for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
print(ctr);
}
Since func's parameter declaration says that f is the name for a pointer to the desired function, we recall first that if f is a pointer then *f is the thing that f points to (i.e. the function print in this case). As a result, just replace every occurrence of print in the loop above with *f:
void func ( void (*f)(int) ) {
for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
(*f)(ctr);
}
}
Source
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.
typedef void (*functiontype)();
Declares a function that returns void and takes no arguments. To create a function pointer to this type you can now do:
void dosomething() { }
functiontype func = &dosomething;
func();
For a function that returns an int and takes a char you would do
typedef int (*functiontype2)(char);
and to use it
int dosomethingwithchar(char a) { return 1; }
functiontype2 func2 = &dosomethingwithchar
int result = func2('a');
There are libraries that can help with turning function pointers into nice readable types. The boost function library is great and is well worth the effort!
boost::function<int (char a)> functiontype2;
is so much nicer than the above.
Since C++11 you can use the functional library to do this in a succinct and generic fashion. The syntax is, e.g.,
std::function<bool (int)>
where bool is the return type here of a one-argument function whose first argument is of type int.
I have included an example program below:
// g++ test.cpp --std=c++11
#include <functional>
double Combiner(double a, double b, std::function<double (double,double)> func){
return func(a,b);
}
double Add(double a, double b){
return a+b;
}
double Mult(double a, double b){
return a*b;
}
int main(){
Combiner(12,13,Add);
Combiner(12,13,Mult);
}
Sometimes, though, it is more convenient to use a template function:
// g++ test.cpp --std=c++11
template<class T>
double Combiner(double a, double b, T func){
return func(a,b);
}
double Add(double a, double b){
return a+b;
}
double Mult(double a, double b){
return a*b;
}
int main(){
Combiner(12,13,Add);
Combiner(12,13,Mult);
}
Pass address of a function as parameter to another function as shown below
#include <stdio.h>
void print();
void execute(void());
int main()
{
execute(print); // sends address of print
return 0;
}
void print()
{
printf("Hello!");
}
void execute(void f()) // receive address of print
{
f();
}
Also we can pass function as parameter using function pointer
#include <stdio.h>
void print();
void execute(void (*f)());
int main()
{
execute(&print); // sends address of print
return 0;
}
void print()
{
printf("Hello!");
}
void execute(void (*f)()) // receive address of print
{
f();
}
Functions can be "passed" as function pointers, as per ISO C11 6.7.6.3p8: "A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1. ". For example, this:
void foo(int bar(int, int));
is equivalent to this:
void foo(int (*bar)(int, int));
I am gonna explain with a simple example code which takes a compare function as parameter to another sorting function.
Lets say I have a bubble sort function that takes a custom compare function and uses it instead of a fixed if statement.
Compare Function
bool compare(int a, int b) {
return a > b;
}
Now , the Bubble sort that takes another function as its parameter to perform comparison
Bubble sort function
void bubble_sort(int arr[], int n, bool (&cmp)(int a, int b)) {
for (int i = 0;i < n - 1;i++) {
for (int j = 0;j < (n - 1 - i);j++) {
if (cmp(arr[j], arr[j + 1])) {
swap(arr[j], arr[j + 1]);
}
}
}
}
Finally , the main which calls the Bubble sort function by passing the boolean compare function as argument.
int main()
{
int i, n = 10, key = 11;
int arr[10] = { 20, 22, 18, 8, 12, 3, 6, 12, 11, 15 };
bubble_sort(arr, n, compare);
cout<<"Sorted Order"<<endl;
for (int i = 0;i < n;i++) {
cout << arr[i] << " ";
}
}
Output:
Sorted Order
3 6 8 11 12 12 15 18 20 22
You need to pass a function pointer. The syntax is a little cumbersome, but it's really powerful once you get familiar with it.
typedef int function();
function *g(function *f)
{
f();
return f;
}
int main(void)
{
function f;
function *fn = g(f);
fn();
}
int f() { return 0; }
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.
#include <stdio.h>
int IncMultInt(int a, int b)
{
a++;
return a * b;
}
int main(int argc, char *argv[])
{
int a = 5;
int b = 7;
printf("%d * %d = %d\n", a, b, IncMultInt(a, b));
b = 9;
// Create some local code with it's own local variable
printf("%d * %d = %d\n", a, b, ( { int _a = a+1; _a * b; } ) );
return 0;
}
Related
I have a quastion about C functions. Is there any possibility to do something like:
#define PRINT_SUM_OF_CONSTS() printSum(10, 5)
void printSum(int a, int b){
print("%d + %d = %d", a, b, a+b);
}
int main(){
void (*pFunc)(void);
pFunc = &PRINT_SUM_OF_CONSTS;
pFunc();
return 0;
}
What I need is to use function which takes two arguments, asign arguments to constants with a macro and use it as function without arguments. Is that somehow possible?
pFunc is a pointer to a function, you cannot create a pointer to a function call with specific parameters.
However, the solution is simple and does not require a macro - you simply create a wrapper function that calls the target function with the desired parameters:
#include <stdio.h>
void printSum(int a, int b)
{
printf("%d + %d = %d", a, b, a+b);
}
void printSumConst() { printSum(10, 5) ; }
int main()
{
void (*pFunc)(void) = printSumConst;
pFunc();
return 0;
}
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?
Declaration
A prototype for a function which takes a function parameter looks like the following:
void func ( void (*f)(int) );
This states that the parameter f will be a pointer to a function which has a void return type and which takes a single int parameter. The following function (print) is an example of a function which could be passed to func as a parameter because it is the proper type:
void print ( int x ) {
printf("%d\n", x);
}
Function Call
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:
func(print);
would call func, passing the print function to it.
Function Body
As with any parameter, func can now use the parameter's name in the function body to access the value of the parameter. Let's say that func will apply the function it is passed to the numbers 0-4. Consider, first, what the loop would look like to call print directly:
for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
print(ctr);
}
Since func's parameter declaration says that f is the name for a pointer to the desired function, we recall first that if f is a pointer then *f is the thing that f points to (i.e. the function print in this case). As a result, just replace every occurrence of print in the loop above with *f:
void func ( void (*f)(int) ) {
for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
(*f)(ctr);
}
}
Source
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.
typedef void (*functiontype)();
Declares a function that returns void and takes no arguments. To create a function pointer to this type you can now do:
void dosomething() { }
functiontype func = &dosomething;
func();
For a function that returns an int and takes a char you would do
typedef int (*functiontype2)(char);
and to use it
int dosomethingwithchar(char a) { return 1; }
functiontype2 func2 = &dosomethingwithchar
int result = func2('a');
There are libraries that can help with turning function pointers into nice readable types. The boost function library is great and is well worth the effort!
boost::function<int (char a)> functiontype2;
is so much nicer than the above.
Since C++11 you can use the functional library to do this in a succinct and generic fashion. The syntax is, e.g.,
std::function<bool (int)>
where bool is the return type here of a one-argument function whose first argument is of type int.
I have included an example program below:
// g++ test.cpp --std=c++11
#include <functional>
double Combiner(double a, double b, std::function<double (double,double)> func){
return func(a,b);
}
double Add(double a, double b){
return a+b;
}
double Mult(double a, double b){
return a*b;
}
int main(){
Combiner(12,13,Add);
Combiner(12,13,Mult);
}
Sometimes, though, it is more convenient to use a template function:
// g++ test.cpp --std=c++11
template<class T>
double Combiner(double a, double b, T func){
return func(a,b);
}
double Add(double a, double b){
return a+b;
}
double Mult(double a, double b){
return a*b;
}
int main(){
Combiner(12,13,Add);
Combiner(12,13,Mult);
}
Pass address of a function as parameter to another function as shown below
#include <stdio.h>
void print();
void execute(void());
int main()
{
execute(print); // sends address of print
return 0;
}
void print()
{
printf("Hello!");
}
void execute(void f()) // receive address of print
{
f();
}
Also we can pass function as parameter using function pointer
#include <stdio.h>
void print();
void execute(void (*f)());
int main()
{
execute(&print); // sends address of print
return 0;
}
void print()
{
printf("Hello!");
}
void execute(void (*f)()) // receive address of print
{
f();
}
Functions can be "passed" as function pointers, as per ISO C11 6.7.6.3p8: "A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1. ". For example, this:
void foo(int bar(int, int));
is equivalent to this:
void foo(int (*bar)(int, int));
I am gonna explain with a simple example code which takes a compare function as parameter to another sorting function.
Lets say I have a bubble sort function that takes a custom compare function and uses it instead of a fixed if statement.
Compare Function
bool compare(int a, int b) {
return a > b;
}
Now , the Bubble sort that takes another function as its parameter to perform comparison
Bubble sort function
void bubble_sort(int arr[], int n, bool (&cmp)(int a, int b)) {
for (int i = 0;i < n - 1;i++) {
for (int j = 0;j < (n - 1 - i);j++) {
if (cmp(arr[j], arr[j + 1])) {
swap(arr[j], arr[j + 1]);
}
}
}
}
Finally , the main which calls the Bubble sort function by passing the boolean compare function as argument.
int main()
{
int i, n = 10, key = 11;
int arr[10] = { 20, 22, 18, 8, 12, 3, 6, 12, 11, 15 };
bubble_sort(arr, n, compare);
cout<<"Sorted Order"<<endl;
for (int i = 0;i < n;i++) {
cout << arr[i] << " ";
}
}
Output:
Sorted Order
3 6 8 11 12 12 15 18 20 22
You need to pass a function pointer. The syntax is a little cumbersome, but it's really powerful once you get familiar with it.
typedef int function();
function *g(function *f)
{
f();
return f;
}
int main(void)
{
function f;
function *fn = g(f);
fn();
}
int f() { return 0; }
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.
#include <stdio.h>
int IncMultInt(int a, int b)
{
a++;
return a * b;
}
int main(int argc, char *argv[])
{
int a = 5;
int b = 7;
printf("%d * %d = %d\n", a, b, IncMultInt(a, b));
b = 9;
// Create some local code with it's own local variable
printf("%d * %d = %d\n", a, b, ( { int _a = a+1; _a * b; } ) );
return 0;
}
i have a simple program like this :
#include<stdio.h>
void add(int *nb)
{
*nb += 1;
}
int f(int nb, void (*add)(int *))
{
if (nb < 5)
f(nb, add(&nb));
return (nb);
}
int main() {
int b = 5;
int a = f(b, add);
printf("%d\n", a);
}
i want to call f recursively until nb become greater or equal to 5, but when i compile the program , the gcc compiler show something like this :
error: passing 'void' to parameter of incompatible type 'void (*)(int *)'
can anyone help me please?
I'm not completely certain as to what you are trying to do with your function, but I took a stab at what I think you might be attempting to do:
#include<stdio.h>
void add(int *nb)
{
*nb += 1;
}
int f(int nb, void (*function)(int *))
{
function(&nb);
if (nb < 5)
f(nb, function);
return (nb);
}
int main() {
int b = 5;
int a = f(b, add);
printf("%d\n", a);
}
Now your function f accepts a function-pointer to a void function that takes an int * as an argument (I renamed this argument to function so it doesn't conflict with your existing function add). It proceeds to call said function and pass in a pointer to nb to function.
What you were doing was passing in the result of add(&nb) into the function f as an argument (which said function is a void so it does not return anything) instead of passing in a pointer to the function.
The line f(nb, add(&nb)); doesn't make sense. You need to call the function somewhere, but not on the same line as where you pass the function pointer on, recursively.
Overall the program doesn't make much sense. I have no idea what you are trying to do, perhaps something similar to this?
#include<stdio.h>
void add(int *nb)
{
*nb += 1;
}
void f(int* nb, void (*add)(int *))
{
add(nb);
if (*nb < 5)
{
f(nb, add);
}
}
int main() {
int a = 5;
f(&a, add);
printf("%d\n", a);
}
Will print 6 since the add is called once. The name add as function parameter to f is also mighty confusing, so better come up with another name for it.
Passing add(&nb) as an argument passes the returned value of add with the argument &nb. The function signature for f requires a function pointer, which can be passed as:
f(nb, add)
Also, even if you were actually trying to pass the returned value of add, that would be undefined behavior since it is of type void.
As Christian pointed out in the comments, the problem is on the recursive call line:
f(nb, add(&nb));
If I understand correctly you are trying to increase nb using the function you receive as a pointer; here however you first pass nb unchanged (and as a value, not by reference), and then call the add function.
In the second argument the compiler is expecting a function pointer, but instead it receives the return value of the add(&nb) function call, which is void.
Also, in case nb is < 5 you need to return the recursive call.
The correct procedure would be:
#include<stdio.h>
void add(int *nb)
{
*nb += 1;
}
int f(int nb, void (*add)(int *))
{
if (nb < 5) {
add(&nb);
return f(nb, add);
}
return (nb);
}
int main() {
int b = 1;
int a = f(b, add);
printf("%d\n", a);
}
It almost seems like you're trying to use lazy evaluation, but C doesn't have that!
I want to initialize an array of size 5 pointers that holds pointers to functions that have no parameters and which returns an int (could be any function that facilitate these requirements).
This is what i tried thus far but i get a syntax error:
int (*func)() fparr[5] = int (*func)();
What is wrong with this syntax?
If the function you want to supply as the default contents of the array is called func, then
you better use a typedef,
you have to use an array initializer
Consider:
typedef int (*IntFunc)(void);
IntFunc fparr[5] = { func, func, func, func, func };
Or the less readable way, if you prefer to avoid typedef:
int (*fparr[5])(void) = { func, func, func, func, func };
Because you are not actually initialising an array of function pointers ... try:
int (*fparr[5])(void) = { func1, func2, func3, func4, func5 };
Step 1:
define the signature of the functions as a type FN:
typedef int (*FN)();
Step2:
define the 5 functions with the FN signature:
int f1(void) { ; }
int f2(void) { ; }
...
Step 3:
define and initialize an array of 5 functions of type FN:
FN fparr[5] = {f1,f2,f3,f4,f5}
otherwise:
If you do not want to define a separate signature, you can do it -- as said before -- so:
int ((*)fpar []) () = {f1,f2, ...}
If you know the number of functions from the array at the moment of declarations, you do not need to write 5, the compiler allocated this memory for you, if you initialize the array at the same line as the declaration.
Well, I'm late...
#include <stdio.h>
int fun0()
{
return 0;
}
int fun1()
{
return 1;
}
int fun2()
{
return 2;
}
int main(int argc, char* argv[])
{
int (*f[]) (void) = {fun0, fun1, fun2};
printf("%d\n", f[0]());
printf("%d\n", f[1]());
printf("%d\n", f[2]());
return 0;
}
An array of function pointers can be initialized in another way with a default value.
Example Code
#include <stdio.h>
void add(int index, int a, int b){
printf("%d. %d + %d = %d\n", index, a, b, a + b);
}
void sub(int index, int a, int b){
printf("%d. %d - %d = %d\n", index, a, b, a - b);
}
int main(){
void (*func[10])(int, int, int) = {[0 ... 9] = add};
func[4] = sub;
int i;
for(i = 0; i < 10; i++)func[i](i, i + 10, i + 2);
}
If you run the above program, you will have the below output. All elements are initialized with function add, but 4th element in array is assigned to function sub
Output
0. 10 + 2 = 12
1. 11 + 3 = 14
2. 12 + 4 = 16
3. 13 + 5 = 18
4. 14 - 6 = 8
5. 15 + 7 = 22
6. 16 + 8 = 24
7. 17 + 9 = 26
8. 18 + 10 = 28
9. 19 + 11 = 30
I just add a bit more to the above answers. Array of function pointers can be indexed by an enum variable, showing the type of operation for each index. Take a look at the following example. Here, we use tyepdef for function pointer operator. Then we create an array of this function pointer called act. Finally, we initialize the array to the increment and decrement functions. In this case, index 0 is referred to increment and index 1 is referred to decrement. Instead of using this raw indexing, we use enum which has INCR, and DECR, corresponding to index 0, 1.
#include<stdio.h>
#include<stdlib.h>
typedef void (*operate)(int *, int);
void increment(int *, int);
void decrement(int *, int);
enum {
INCR, DECR
};
int main(void){
int a = 5;
operate act[2] = {increment,decrement};
act[INCR](&a,1);
printf("%d\n",a);
act[DECR](&a,2);
printf("%d\n",a);
return 0;
}
void increment(int *a, int c){
*a += c;
}
void decrement(int *a, int c){
*a -= c;
}
Here is a working example showing the correct syntax:
#include <stdio.h>
int test1(void) {
printf("test1\n");
return 1;
}
int test2(void) {
printf("test2\n");
return 2;
}
int main(int argc, char **argv) {
int (*fparr[2])(void) = { test1, test2 };
fparr[0]();
fparr[1]();
return 0;
}
Example code:
static int foo(void) { return 42; }
int (*bar[5])(void) = { foo, foo, foo, foo, foo };
Note that the types int (*)() and int (*)(void) are distinct types - the former denotes a function with a fixed but unspecified number of arguments, whereas the latter denotes a function with no arguments.
Also note that the C declarator syntax follows the same rules as expressions (in particular operator precedence) and is thus read inside-out:
bar denotes and array (bar[5]) of pointers (*bar[5]) to functions (int (*bar[5])(void)). The parens (*bar[5]) are necessary because postfix function calls bind more tightly than prefix pointer indirection.
How is it possible to give a function (B) a function (A) as a parameter?
So that I can use function A in the function B.
Like the Variable B in the following example:
foo(int B) { ... }
By using function pointers. Look at the qsort() standard library function, for instance.
Example:
#include <stdlib.h>
int op_add(int a, int b)
{
return a + b;
}
int operate(int a, int b, int (*op)(int, int))
{
return op(a, b);
}
int main(void)
{
printf("12 + 4 is %d\n", operate(12, 4, op_add));
return EXIT_SUCCESS;
}
Will print 12 + 4 is 16.
The operation is given as a pointer to a function, which is called from within the operate() function.
write function pointer type in another function's parameter list looks a little weird, especially when the pointer is complicated, so typedef is recommended.
EXAMPLE
#include <stdio.h>
typedef int func(int a, int b);
int add(int a, int b) { return a + b; }
int operate(int a, int b, func op)
{
return op(a, b);
}
int main()
{
printf("%d\n", operate(3, 4, add));
return 0;
}
Lets say you have a function you want to call:
void foo() { ... }
And you want to call it from bar:
void bar(void (*fun)())
{
/* Call the function */
fun();
}
Then bar can be called like this:
bar(foo);