Callback functions

Can someone explain to me what is and how to use callback functions. I find them quite confusing. An explanation or directions to a good tutorial would be much appreciated, Thanx :)

Comments

  • : Can someone explain to me what is and how to use callback functions. I find them quite confusing. An explanation or directions to a good tutorial would be much appreciated, Thanx :)
    :
    What's a callback function?
  • : : Can someone explain to me what is and how to use callback functions. I find them quite confusing. An explanation or directions to a good tutorial would be much appreciated, Thanx :)
    : :
    : What's a callback function?
    :

    Exacly. I thought someone else might be able to answer that for me. I think it has something to do with passing the adress of a function as an argument of another function, although im not sure
  • : Can someone explain to me what is and how to use callback functions. I find them quite confusing. An explanation or directions to a good tutorial would be much appreciated, Thanx :)
    :

    are you sure that you want to know that?

    http://www.xtremejava.com/books/TICPP-2nd-ed-Vol-one.zip

    Go to chapter 11

    Good luck !!!
  • : : What's a callback function?
    :
    : Exacly. I thought someone else might be able to answer that for me. I think it has something to do with passing the adress of a function as an argument of another function, although im not sure

    That's exactly it. You pass a function pointer to another function, so that it may "call you back". A simple example is SetTimer in the Win32 API. You simple pass SetTimer the address of a function and tell it how often to call it, and it does. A more complicated example is 'qsort' in the C standard library. You pass it a function which it calls to compare elements in an array that it knows nothing about.

    Here's an example of a function that takes a callback. 'for_each' receives two arguments: a pointer to a string and the address of a function to call on each character in that string.
    [code=ffffff]
    [color=a020f0]#include [/color][color=bb0000][/color]
    [color=a020f0]#include [/color][color=bb0000][/color]

    [color=000080]void[/color] for_each ([color=000080]char[/color]* s, [color=000080]int[/color] (*callback)([color=000080]int[/color])) {
    [color=000000][b]for[/b][/color] (; *s; ++s)
    *s = callback(*s);
    }

    [color=000080]int[/color] main()
    {
    [color=000080]char[/color] s[] = [color=bb0000]"This Is A Test"[/color];

    puts (s);

    for_each (s, toupper);
    puts (s);

    for_each (s, tolower);
    puts (s);

    [color=000000][b]return[/b][/color] [color=bb0000]0[/color];
    }
    [/code]
    Cheers,
    Eric


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories