#include<iostream>
int fx1(void(*p_funct)(int))
{
(*p_funct)(10);
}
int fx2(int a)
{
std::cout<<"FX2";
a = a *a;
std::cout<<a;
}
int main()
{
int(*p_funct)(int); //make ptr
p_funct = fx2; //assign to a function
fx1(p_funct); //caller with pointer to function
return 0;
}
In function 'int main()':
..\src\main.cpp:19:13: error: invalid conversion from 'int (*)(int)' to 'void (*)(int)' [-fpermissive]
fx1(p_funct); //caller with pointer to function
/*
A pointer to a function that takes an int as a parameter
and returns nothing.
*/
int fx1(void(*p_funct)(int))
/*
A pointer to a function that takes an int as a parameter
and returns an int.
*/
int(*p_funct)(int);
Also fx1 and fx2 have no return value.
In fx1, you call the function like so:
(*p_funct)(10);
but in main, you call it like so:
fx1(p_funct);
Keep your style consistent. I would recommend that you call the function like you do in main. It looks cleaner and makes it more obvious what you are doing.
#include<iostream>
int fx1(int(*p_funct)(int))
{
(*p_funct)(10);
}
int fx2(int a)
{
std::cout<<"FX2"<<std::endl;
a = a *a;
std::cout<<a;
return a;
}
int main()
{
int(*p_funct)(int); //make ptr
p_funct = fx2; //assign to a function
fx1(p_funct); //caller with pointer to function
return 0;
}