what's a callback function?

Posted on 2004-11-04
Last Modified: 2011-10-03
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?

Question by:daniel416
    LVL 4

    Assisted Solution

    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.

    LVL 5

    Assisted Solution


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

    LVL 9

    Accepted Solution

    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()
        cout << "hello Daniel " << endl;
        return 0;

    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++)

    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;

    LVL 22

    Assisted Solution

    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.

    LVL 3

    Author Comment

    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.
    LVL 3

    Author Comment

    @ zaghaghi  :
     what's a qt-platform?
    LVL 9

    Expert Comment


    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;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now