4

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.

3
  • Forget my C++ answer, I just realized you were asking about C. Commented Oct 25, 2014 at 6:21
  • The line of code you presented does the following: the function abc() is invoked. It has a parameter that is type double. (there is no need to state the returned value from the function abc() is double, because that info is already stated in the abc() function and in the prototype for the abc function. Note, I don't see the prototype for the abc() function, so expect that you just did not include that line. The function test() is expecting a parameter of type double. Note: I don't see the prototype for the test() function, so expect that you jsut did not include that line. Commented Oct 27, 2014 at 19:51
  • The result of the code is the parameter to the test() function is being obtained from the return value of the embedded call to the abc() function.. Commented Oct 27, 2014 at 19:53

2 Answers 2

4

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."

Sign up to request clarification or add additional context in comments.

Comments

-1

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

2 Comments

FYI ,The question is tagged C not C++.
This doesn't really answer the question. The OP asked about passing a function to another function without using a pointer. Both your examples use pointers implicitly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.