Callback function?

P

Pradeep

Hi all,
Can any one explain me what is callback function....
I have written some code after reading some tutorials from internet...
But I am not sure is it a right way to write a call back function...
I have mentioned my doubts in code comments.
My code is...

void myfun_callback(void(*fp)(char*)) //is this function is a call
back?
{
(*fp)("Pradeep Kumar");
}

void A(char *msg) //function A,called by myfun_callback
{
cout<<"Function A: "<<msg<<endl;
}

void B(char *msg) //function A,called by myfun_callback
{
cout<<"Function B: "<<msg<<endl;
}

int main()
{
void(*fp)(char*); //function pointer
fp=A; //giving address of function A
myfun(fp); //right way to call the callback function?
fp=B; //giving address of function B
myfun(fp); //right way to call the callback function?
return 0;
}

ur help will be appreciated..
Thank u in advance

Regards
Pradeep Kumar Basrani
 
V

Victor Bazarov

Pradeep said:
Can any one explain me what is callback function....

A callback function is a function pointer to which is supplied to (and
stored by) the system (whatever the system is) to be called at some
indetermined time, generally speaking. Sometimes the time at which the
callback is called is a bit clearer defined, but it doesn't mean the
path to the callback is (or can be) determined.
I have written some code after reading some tutorials from internet...
But I am not sure is it a right way to write a call back function...
I have mentioned my doubts in code comments.
My code is...

void myfun_callback(void(*fp)(char*)) //is this function is a call
back?

No. This function is not a callback _unless_ you give it to some
other function to be called.
{
(*fp)("Pradeep Kumar");
}

void A(char *msg) //function A,called by myfun_callback

This function is most likely a callback.
{
cout<<"Function A: "<<msg<<endl;
}

void B(char *msg) //function A,called by myfun_callback

This is another function that is likely a callback.
{
cout<<"Function B: "<<msg<<endl;
}

int main()
{
void(*fp)(char*); //function pointer
fp=A; //giving address of function A
myfun(fp); //right way to call the callback function?

It's a way to call a function that will *in turn* call the
callback you supply to it.
fp=B; //giving address of function B
myfun(fp); //right way to call the callback function?
return 0;
}

ur help will be appreciated..
Thank u in advance

Try to use English here and avoid chatroomisms.

V
 
E

Ehud Shapira

A callback function is a function in your code that is called by
something outside of your control; it could be the operating system,
or it could be a 3rd party library you are using.

This typically means you first give out a pointer to that function to
the external caller.

Other than that, it's a function like any other, but of course, it
should work according to what the external caller expects.
 
J

James Kanze

A callback function is a function pointer to which is supplied to (and
stored by) the system (whatever the system is) to be called at some
indetermined time, generally speaking. Sometimes the time at which the
callback is called is a bit clearer defined, but it doesn't mean the
path to the callback is (or can be) determined.

This is generally known as inversion of control. Basically, if
the "system" is a service provider, and you are client of that
service, you "call" the service, and thus "control" what happens
when. If the service needs to call you, e.g. to notify you of
an asynchronous event (observer pattern), or just to allow
customization, then there is inversion of control. The
important point in inversion of control, of course, is that
although the service provider must call the client, it knows
nothing of the client. Some some means of indirection is called
for.

In classical OO technology, the solution is an abstract base
class (an "interface"). This interface is provided by the
service provider; the client derives from it, and provides the
service with a pointer to an instance of its derived class.
IMHO, this is still the preferred solution most of the time in a
pure C++ environment. If the service is very, very simple,
however, and the client action will be fixed at compile time, a
template can be used for the service. In general, however, this
should be avoided because of the lack of flexibility and the
compile time coupling. (Because the template solution uses duck
typing, of course, it can require less work, at least in terms
of typing, on the part of the client. Again, this is mostly
relevant for very simple services and actions, and not always
then.)

When interfacing with a C API, of course, neither the abstract
base class nor the template are options. So we simulate using a
pointer to a function, and---in well designed systems, at
least---a pointer to void in order to provide additional client
data. Unless there is a requirement that the service be able to
be called from a C program, I would recommend one of the other
solutions, however; they're a lot cleaner and significantly less
error prone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top