Thread: callback function v. regular function

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663

    callback function v. regular function

    Hi,

    Every time I see the term callback function, my eyes glaze over, and I know I no longer really understand what the author is talking about.

    In three sentences or less, can someone explain what distinguishes a call back function from a regular function?

    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    real quick. it's when you pass the address of a function to another function and it gets called.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ok. i'm bored. i'll explain some more. (i could be mistaken, but....)

    an example is, if you're using MFC, there's a class which describes a Button. it's kind of a dumb class. it only knows when the button is pressed, mouse-overed, etc. at some point, when a button is clicked, you want it to actually do something (such as submit a form). since the button class is already defined and needs to be used over and over again for many different situations, you can't just build the functionality into the button. but, you can have a variable within the class which holds the address of another function. this is a function you create (typically). it's the function that you want to be executed when the button is pressed.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I liked your first answer the best, but I've read about the important implications contained in your second answer.

    Thanks.

  5. #5
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    It's a kind of relationship between the program and the OS, the program tells the OS when to call a certain function (a callback), and the OS will then (hopefully ) do so when the time comes.

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by BMJ
    It's a kind of relationship between the program and the OS, the program tells the OS when to call a certain function (a callback), and the OS will then (hopefully ) do so when the time comes.
    That's right
    Consider this :
    Code:
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    ....
    }
    CALLBACK is defined as __stdcall which refers to a special calling sequence for function calls that occur between Windows itself and your application.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    *stands corrected* (for the most part)
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    >> calling sequence

    isnt it calling convention?
    like by conventions arguments are passed in right to left order and the callee (called function) cleans up the stack?
    signature under construction

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Quote Originally Posted by Hunter2 in the other thread, since CBoard Mailer is retarded...
    It's a user-specified function, usually set via a function pointer, that gets called by the inner workings of an independent module when a given event occurs.
    I don't think 'callback' is defined in relation to the calling convention; CALLBACK is just what Microsoft likes to use for their callback functions. Also, a callback doesn't necessarily involve the OS. For an example, see the thread on reference-counting smart pointers here - my implementation uses a callback function to know what to do when the reference count hits zero, whether to use delete, delete[], or to take some other user-defined action.

    **EDIT**
    NVM, all of the implementations seem to use a callback of some sort; mine and codeplug's original use function pointers, while some others used a functor class as a template parameter instead.
    Last edited by Hunter2; 03-24-2005 at 11:38 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. How do wait till a callback function returns
    By poorkoder in forum C Programming
    Replies: 1
    Last Post: 06-16-2008, 11:22 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Pointer to member function as callback function
    By ninebit in forum C++ Programming
    Replies: 10
    Last Post: 03-01-2002, 05:52 AM