what's a callback function?

hi developers,

a newbie question:
what's a call back function? and how do i implement one?
any examples on the web that have been useful to you?

Thanks!
daniel.
LVL 3
daniel416Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pankajtiwaryCommented:
Hi daniel416,

A callback function is a normal function but it is not called normally. It is registered with the application to respond to some event. You can think of it as a kind of trigger which is executed when some thing happens. These are implemented using function pointer. A normal usage that I can remember is the signal() function on *nix machine. This function takes 2 parameters, first is the signal and the other is the function pointer. So, whenever that particular is generated, that function is called.

Similarly atexit() also registers a callback function which is executed when the exit() system call is encountered. If you specify exactly what you want to do, we will be in a better position to help you.

Hope this helps.

Cheers!
0
dennis_georgeCommented:
Hi,

check the following site for callback function... You will get all your answers here....

http://www.function-pointer.org/callback.html

Dennis
0
Hamed ZaghaghiProgrammerCommented:
Hi daniel416,

comment from pankajtiwary, is true, but i have give you some extra informaion:
in some functions in c/c++ use the function pointer to call it when needed, for example:

void myfunction()
{
    cout << "this function called at exit" << endl;
}

int main()
{
    atexit(myfunction);
    cout << "hello Daniel " << endl;
    return 0;
}

output--------------------------------------
hello Daniel
this function called at exit
---------------------------------------------

and also in MFC, there is a lot of callback functions,
and also in Qt platform for c/c++, in Qt callback functions named Slot,
and you can connect signals to slots, by `connect' function,

another example of functions that use a call back function in its argument is `qsort' function.

an example of function that use callback function:


void printcall(int n)
{
   cout << n << endl;
}


void count(int start, int end,void(*callfunc)(int))
{
   int i;
   for(i=start; i<end; i++)
      callfunc(i);
}



in above example, i use callback function in function count.
when you want to define a function pointer, you can use following format:

RET (*FUNCNAME) ( ARG1, ARG2, ...)

RET :  return value of your callback function,
FUNCNAME : function name, or type name, if you want to use it to define a new type ,
ARG1, ARG2, .. : first argument,second argument, and ...

in the following i define a new type of a function that return an int value, and give 2 argument of type int:

typedef (int) (*mytype) (int, int);

void func1(int first, int second, mytpe callfunc)
{
    cout << callfunc(first, second) << endl;
}

int mycallback(int n, int m)
{
    return n+m;
}

int main()
{
   int i,j;
   for(i=0; i<10; i++)
      for(j=0; j<10; j++)
         func1( i, j, mycallback );
   return 0;
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

grg99Commented:
Things to watch out about callbacks:

(1)  The callback function has to be declared EXACTLY as is documented.  The calling code has no way of checking  this, so if you make the slightest deviation (stdcall instead of PASCAL, NEAR instead of FAR, etc...) something bad will happen.

(2) In C, the syntax for function pointer dclarations is mighty confusing.  Either copy and paste a known working one, or very very very carefully read up on this.

(3)  A callback is sometimes called from a "funny" place, perhaps a system routine that is running at a funny priority level, or with a different or very small stack, or from a caller that has turned off interrupts.  There are often documented restrictions, such as "don't allocate more than 100 bytes of local variables (small stack)", or "dont do any I/O instructions".  Heed these.

(4)  Callbacks are often called from time-critical system routines.  Don't linger inside your callback any more than necessary.

(5) callbacks are often restricted to a small set of system API's.  Heed these restrictions.

(6)  callbacks may call something that ends up calling the callback again.  Either add some recursion counters to prevent this, or make sure your callback can handle recursive calls.

0
daniel416Author Commented:
wow,
thanks everyone,
i'll need a bit of time to digest this info.
i'll get back to you when i have more comments.
please post if you have something to add.
daniel.
0
daniel416Author Commented:
@ zaghaghi  :
 what's a qt-platform?
0
Hamed ZaghaghiProgrammerCommented:
hi,

there are several platforms for c/c++, so with this language you can program for many platforms,
one of the best platforms for c/c++ is Trolltech Qt, its compiler is free and available in its site and also with most linux packages, like fedora core.
in this platform you can write your program in c/c++ with qt classes and functions, and then compile it under operating systems like Linux, Windows, Mac OS, and other OS that Qt supports.

Qt gathers a very large library like in .net platform, and like one that MFC prepair for you, like:

QDialog, QButton, QLabel, and ......

have a good progamming day;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.