Reputation: 183
My instructor mentioned using function as parameter in other function. (I don't mean using pointers. Is it possible ? I show below) I don't understand what he did. Can anyone explain with examples ? Thank you all appreciated answers.
using style is:
int test(double abc(double)){
// bla bla
}
function is:
double abc(double n){
// function main
}
The examples is written by me I'm not so sure they're right.
Upvotes: 4
Views: 138
Reputation: 723
if you use a pointer you can call the function later in function test.
typedef double (*func_type)(double);
int test(func_type func) {
// bla bla
cout << func(3);
}
// 2 call
test(double_func)
If you want to call the function before calling test, then just define:
int test(double) {
// bla bla
cout << double;
}
// 2 call
test(double_fun(2.0));
correct choice depends on your intentions
Upvotes: -1
Reputation: 145829
This function declaration:
int test(double abc(double))
{
// bla bla
}
is equivalent to:
int test(double (*abc)(double))
{
// bla bla
}
The abc
parameter is a parameter of function pointer type (double (*)(double))
).
For C Standard reference:
(C99, 6.7.5.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."
Upvotes: 4