[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

what's a callback function?

Posted on 2004-11-04
7
Medium Priority
?
1,077 Views
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?

Thanks!
daniel.
0
Comment
Question by:daniel416
7 Comments
 
LVL 4

Assisted Solution

by:pankajtiwary
pankajtiwary earned 600 total points
ID: 12500331
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
 
LVL 5

Assisted Solution

by:dennis_george
dennis_george earned 400 total points
ID: 12500468
Hi,

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

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

Dennis
0
 
LVL 9

Accepted Solution

by:
Hamed Zaghaghi earned 600 total points
ID: 12501107
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 22

Assisted Solution

by:grg99
grg99 earned 400 total points
ID: 12504045
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
 
LVL 3

Author Comment

by:daniel416
ID: 12506438
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
 
LVL 3

Author Comment

by:daniel416
ID: 12506448
@ zaghaghi  :
 what's a qt-platform?
0
 
LVL 9

Expert Comment

by:Hamed Zaghaghi
ID: 12508178
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

872 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