I have a function which has a function pointer input. I can easily give function names to it as input. But I wonder if it's possible to define a function as input. For example I have a function like this;
void exampleFunction ( void (*functionPointer)( void ) ) {
codes
...
}
Can I give an input like this inside the brackets? For example;
exampleFunction( void helloFunction ( void ) {
printf("Hello");
} );
It gives compilation error like this but is there any other ways to do it?
Nested function is not supported by C because we cannot define a function within another function in C.
Explanation: A function cannot be defined inside the another function, but a function can be called inside a another function.
A nested function is a function defined inside the definition of another function. It can be defined wherever a variable declaration is permitted, which allows nested functions within nested functions. Within the containing function, the nested function can be declared prior to being defined by using the auto keyword.
We cannot pass the function as an argument to another function. But we can pass the reference of a function as a parameter by using a function pointer. This process is known as call by reference as the function parameter is passed as a pointer that holds the address of arguments.
This is impossible in C.
In C++, you can use a lambda-expression:
exampleFunction([](){ std::cout << "Hello"; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With