Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here, what is the meaning of this?

Can we pass the function as a parameter to the other function ?


like this---

int func2(parameter3,parameter4)
{
// code
}

void func1(parameter1,parameter2,func2) // why it is not int func2( )?
{
// code
}

Here Are we calling fun2( ), then why there are no brackets and actual parameters to func2( )?

What I have tried:

How could we pass like that ?
.........
Posted
Updated 26-Jun-21 22:15pm

it is a common pattern in the Apple world to work with such constructs. Such functions are called completion handler. The main reason is improve usage of threads and use the function result of lengthly operations like downloads. Read this article for details.
 
Share this answer
 
C
int foo ( int (*f)(int) )
   {
   return f(666);
   }

This means that foo is a function returning an integer that takes a function f as a parameter. f will be a pointer to a function which returns an integer and which takes a single int parameter.

When calling a function with a function parameter, the value passed must be a pointer to a function. Just use the function's name (without parentheses) for this:
C
int myFunct(int x)
   {
   return x * 2;
   }
...
printf("%u\n", foo(myFunct));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900