Home > Mobile >  How to pass a void function as a parameter c ?
How to pass a void function as a parameter c ?

Time:11-19

I am trying to pass a function as a parameter. Here, I created a function sortingRunTime() that takes an array and a function as a parameter and prints out the execution time.

void sortingRunTime(int* arr, void(*functiontocall)(int* arr))
{
    clock_t t = clock();
    (*functiontocall)(arr);
    t = clock() - t;
    cout << (t / (float)CLOCKS_PER_SEC);
}

I have another function void bubblesort(int* arr), that sorts the array and doesn't return anything. Also, I have other sorting functions with the type of void and I have to use sortingRunTime() for all of the sorting functions. In the main function, I have

sortingRunTime(arr, bubblesort(arr));

but it gives an error message "argument of type "void" is incompatible with parameter of type "void (*)(int *arr)"". How can I fix it?

CodePudding user response:

If you call a function that returns void, the result of the function call is void, and you cannot pass that void as an argument into sortingRunTime.

sortingRunTime expects a (pointer to) a function as the argument. So, instead of passing the result of the function call, pass the (pointer to the) function itself:

sortingRunTime(arr, &bubblesort);

Since function implicitly converts to a pointer to itself, the use of addressof operator is optional. It works the same without it:

sortingRunTime(arr, bubblesort);

CodePudding user response:

You need to pass the function itself not the result of the function call.

So instead of

sortingRunTime(arr, bubblesort(arr));

that actually evaluates to the call

sortingRunTime(arr, void);

that is incorrect and does not make a sense you need to write

sortingRunTime(arr, bubblesort);

And within the function sortingRunTime it is simpler to call the function like

functiontocall(arr);

Pay attention to that these function declarations

void sortingRunTime(int*, void(*functiontocall)(int* ));

and

void sortingRunTime(int*, void functiontocall(int * ) );

are equivalent and declare the same one function.

To make the difference between thsese calls

sortingRunTime(arr, bubblesort(arr));

and

sortingRunTime(arr, bubblesort);

more visible consider the following demonstration program.

#include <iostream>
#include <iomanip>
#include <type_traits>

void bubblesort( int * );

int main() 
{
    int a[10];
    
    std::cout << std::boolalpha 
              << std::is_same<void, decltype( bubblesort( a ) )>::value 
              << '\n';
              
    std::cout << std::boolalpha 
              << std::is_same<void( int * ), decltype( bubblesort )>::value 
              << '\n';

    return 0;
}

The program output is

true
true

As it is seen the type of the expression bubblesort( a ) is void while the type of the expression bubblesort is the function type void( int * ). This expression (function designator) used as a function argument is implicitly converted to a pointer to the function of the type void ( * )( int * ).

  • Related