Solved

CallBack functions

Posted on 2011-09-08
12
249 Views
Last Modified: 2012-06-27
what is advantage of using call back fuction instead of normal functions.
when we can use normal functions why we have to use call back functions
0
Comment
Question by:nagaharikola
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 500 total points
ID: 36510442
There is no specific advantage per se.

A callback function is just a function that is called indirectly via a pointer. The difference is how and when it is called. With a callback the function is passed by pointer into some other thread of code. That other code can then call the "callback" function via this pointer. This allows you to decide what the function is that gets called.

Think of it as a phone number that you give to someone. Depending on the number, when they call it they will get a different person. With the function pointer, then they call it they will get a different function depending on which function's address you passed in.

A very good tutorial on function pointers: http://www.newty.de/fpt/index.html
0
 
LVL 6

Expert Comment

by:theKashyap
ID: 36511694
Prime usecase for callback functions is asynchronous communication. Where as "normal" functions are used in synch communication (single thread).
E.g. you write a small library that hides all the complexity of C-socket APIs and provides a more user friendly set of functions. Idea being that you call one function from this library and provide all connection params (like socket num, IP,...) and a callback function pointer. Then that function will take care of setting up the connection & monitors incoming data by setting up a thread, when the data comes it'll call back your function via pointer.
Given what evilrix described, now this library can be used by anyone.

In OO design, callback functions are used to break circular dependencies.
0
 
LVL 16

Expert Comment

by:imladris
ID: 36511766
The other point, implicit in the phone analogy, is that not only do you get to choose *how* your code will respond to the call back (by selecting which function to pass for the call back) but it also allows the "other" code (usually the operating system, or some package that was bought) to determine *when* to call your function.

This can be used for interim progress reports on the task it is working on. Or, in the operating system case, it is often explicitly used to manage a timer. Whenever a time interval expires, it can call back to your code to notify you of that.
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 36512969
Well, there's no one way I can think to describe Callback functions:

They are a way of implementing something today that will work with something that won't exist until the future.  

They are a different way of implementing reusable code.  

At a concept level, they are an efficient way of implementing run-time dynamic linking without the overhead that normally comes with run-time linking.


A perfect example would be the generic sorting function QuickSort().  When it was written, the programmer knew what sorting algorithm he wanted to implement.  But he didn't know what would be sorted.  So there was no way that logic could be written into QuickSort to compare items to determine which comes first in the sort.  So a Callback function was designed into QuickSort so the user could supply the missing logic that compares the items to being sorted.

So utilizing a Callback function, a programmer was able to write a sorting algorithm without knowing what was getting sorted, and another programmer could sort his data without knowing HOW to implement sort logic (by supplying the compare logic via a Callback).  Effectively two pieces of logic got "linked' together at run-time by simply supplying a pointer to a Callback function.
0
 

Author Comment

by:nagaharikola
ID: 36521471
I am not clear on this point yet.
how it is different from normal function.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36521663
>> I am not clear on this point yet.
>> how it is different from normal function.

As I said in my [the] very first comment... it's not different.

http:#a36510442

"A callback function is just a function that is called indirectly via a pointer. "

Do you understand what a pointer is, what a pointer to a function is and what it means to call a function via a pointer?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:nagaharikola
ID: 36521698
yes i understand that.
the link given is not clear for me.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36522886
>> the link given is not clear for me.
Specifically, what is not clear? What don't you understand?
0
 
LVL 6

Expert Comment

by:theKashyap
ID: 36523517
>> I am not clear on this point yet.
>> how it is different from normal function.
Think of it more like a role that a function can play.

E.g. take a number 12. It's a number, but it could be my birth date or it could be distance in miles from your home to your office or it could be number of volleyball players allowed in a team.
So its the same, but it can play many roles.

Another example: A cop can be:
- a father
- a friend
- a traffic cop
- a federal cop
- ...

A function is just a function, when you use it in some special cases (e.g. the passing it as a param to a sort algorithm) it acts as a callback function. So a callback function is also a "normal function".
0
 

Author Comment

by:nagaharikola
ID: 36527351
>>callback function is also a "normal function".
   if callback function is a normal function why we have to go for it.


Do you mean defination of  call back function changes for different scenarios.
please explain with a code snippet  


0
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 36528082
>>  if callback function is a normal function why we have to go for it
You don't have to do anything.

Ok, look at it this way... a door can be open or it can be closed but in either situation it is still a door. Same with a callback function. It can be called directly or indirectly (via a pointer as a callback) but it is still just a function. The term "callback" refer to how you call the function it does not imply a special type of function.

>> Do you mean defination of  call back function changes for different scenarios.
As a question there is no real sensible answer to this because it implies that if a function is being used as a callback it must have some special properties. This isn't the case. It's just a function -- called via a pointer.

>> please explain with a code snippet  
Sure, see below.


#include <stdio.h>

typedef void (*func_ptr)();

void foo()
{
   puts("hello, world");
}

void bar(func_ptr callback)
{
   /* performing a function callback */
   callback();
}

int main()
{
   /* calling foo direct */
   foo();

   /* calling foo via a pointer */
   func_ptr fp = foo;
   fp();

   /* passing the functon pointer to bar() so it can be used as a callback */

   bar(fp);

   return 0;
}

Open in new window

0
 

Author Comment

by:nagaharikola
ID: 36529161
thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

705 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

19 Experts available now in Live!

Get 1:1 Help Now