Callback functions

P

Pratik

what are callback functions? Where we require callback functions? In
what scenario we require callback functions?
 
P

Phlip

Pratik said:
what are callback functions?

I send you a letter. Inside is a self-addressed stamped envelop. You put
something inside that, and drop it in the mail. It comes back to me.
Where we require callback functions?

Callbacks are the simplest form of the Observer Pattern from the book
/Design Patterns/. Google for those.
In what scenario we require callback functions?

When another module must trigger behavior in my module, without I know when
the trigger will happen.

In an event driven architecture, such as a GUI, code paints a window and
then awaits user events. The user clicks on controls, and the window's event
dispatcher calls functions assigned to those controls. Some are functions
that you wrote, providing the application behavior behind your window.
 
J

JKop

Pratik posted:
what are callback functions? Where we require callback functions? In
what scenario we require callback functions?

If only one program is running on a system, there's no requirement.


But consider a system where two or more programs are running.

Program 1 is the main program. It controls the user interface on the front
of the safe and it controls the open/close mechanism for the door.

Program 2 controls the lock mechanism on the safe, nothing more.


Both programs are running.

Someone comes over to the safe to open it and take cash out. Program 1 must
communicate with Program 2 to tell it to unlock the safe. To do such a
thing, it needs the address of a function.


Here's what Program 2 looks like:


void Unlock(void)
{
//blah
}


Program 1 will want something like the following in it:

int main(void)
{
void (*Unlock)(void);

Unlock = GetAddressOfUnlockFunctionInProgram2();

Unlock();
}


-JKop
 
R

red floyd

Pratik said:
what are callback functions? Where we require callback functions? In
what scenario we require callback functions?

Sounds like a homework question.
 
E

E. Robert Tisdale

Pratik said:
what are callback functions? Where we require callback functions?
In what scenario we require callback functions?

#include <stdlib.h>

void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void*, const void*));

compar(const void*, const void*) is a callback function.

#include <signal.h>

typedef void (*sighandler_t)(int);

sighandler_t signal(int signum, sighandler_t handler);

handler(int) is a callback function.

#include <stdio.h>

typedef struct X {
int i;
void (*f)(const X*);
} X;

void f(const X* p) {
fprintf(stdout, "%d = p->i\n", p->i);
}

void g(const X*p) {
p->f(p);
}

int main(int argc, char* argv[]) {
const
X x = {13, f};
g(&x);
return 0;
}

f(const X*) is a callback function.

#include <iostream>

class X {
private:
int I;
public:
virtual
void g(void) {
std::cout << I << " = I" << std::endl;
}
explicit
X(int i): I(i) { }
};

int main(int argc, char* argv[]) {
X x(13);
x.g();
return 0;
}

X::g(void) calls the corresponding callback function
from the virtual function table.
 

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

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top