How do I pass a function into another function

so I am trying doing a project right now that reqires me to pass a function into another function. How do i go about doing that"?

when I did
1
2
3
4
5
6
7
8
9
void functionA(//function with some parameters)
{
    .....
}

bool check(functionA(), parameter1, parameter 2)
{
    ........
}


however, this doesn't seem to be working. On the error, it says,
1
2
3
4
5
expected ')' before ',' token

and

error: expected initializer before parameter 1


How do i do such a thing. I know i probably have to use a pointer, but how?

Thanks
--
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

double foo( int a, double b ) { std::cout << "foo( " << a << ", " << b << " )\n" ; return a+b ; }
double bar( int a, double b ) { std::cout << "bar( " << a << ", " << b << " )\n" ; return a*b ; }

using fn_type = double( int, double ) ; // type of the function

double foobar( fn_type& fn /* reference to function */, int arg1, double arg2 )
{ std::cout << "foobar => " ; return fn(arg1, arg2 ) ; }

// polymorphic version of the above
template < typename FN, typename... ARGS > auto generic_foobar( FN&& fn, ARGS&&... args )
{ std::cout << "generic_foobar => " ; return std::forward<FN>(fn)( std::forward<ARGS>(args)... ) ; }

const char* baz( int a, char b, double c )
{ std::cout << "baz( " << a << ", " << b << ", " << c << " )\n" ; return "baz" ; }

int main()
{
    foobar( foo, 1, 2.34 ) ;
    foobar( bar, 5, 6.78 ) ;
    generic_foobar( bar, 5, 6.78 ) ;
    generic_foobar( baz, 9, 'A', 1.23 ) ;
    generic_foobar( []( auto v ) { std::cout << "closure(" << v << ")\n" ; return v ; }, 1234 ) ;
}

http://coliru.stacked-crooked.com/a/ceeef8040e927a13
why do you wanna do that, by the way?
In keeping with the template function generic_foobar() I was wondering if the lambda-version of generic_foobar() could also take a universal reference and whether or not that makes any difference:
 
generic_foobar( []( auto&& v ) { std::cout << "closure(" << v << ")\n" ; return v ; }, 1234 ) ;
I seem to keep getting an error.

1
2
3
4
5
6
7
error: invalid use of type 'void' in parameter declaration
error: cannot declare reference to 'void'
bool checkCoord(void &displayBoard, coordinates& row, coordinates& cols)

//trying to pass a function called display board
//coordinates is a struct function where I am passing the rows and cols


the reason for this is the project tells me to:

Add a boolean value-returning function to check the status of the coordinates
selected by the user so as to return a boolean to indicate whether or not a mine
was hit (i.e., if the user selected a square containing a mine, this function would
return true; otherwise, it would return false). At a minimum, you will pass the
game board as well as the structure variable containing the row-column
coordinate described in #2 above to this function.
Topic archived. No new replies allowed.