473,747 Members | 2,260 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Callback function?

Hi all,
Can any one explain me what is callback function....
I have written some code after reading some tutorials from internet...
But I am not sure is it a right way to write a call back function...
I have mentioned my doubts in code comments.
My code is...

void myfun_callback( void(*fp)(char* )) //is this function is a call
back?
{
(*fp)("Pradeep Kumar");
}

void A(char *msg) //function A,called by myfun_callback
{
cout<<"Function A: "<<msg<<end l;
}

void B(char *msg) //function A,called by myfun_callback
{
cout<<"Function B: "<<msg<<end l;
}

int main()
{
void(*fp)(char* ); //function pointer
fp=A; //giving address of function A
myfun(fp); //right way to call the callback function?
fp=B; //giving address of function B
myfun(fp); //right way to call the callback function?
return 0;
}

ur help will be appreciated..
Thank u in advance

Regards
Pradeep Kumar Basrani

Jun 27 '07 #1
2 2633
Pradeep wrote:
Can any one explain me what is callback function....
A callback function is a function pointer to which is supplied to (and
stored by) the system (whatever the system is) to be called at some
indetermined time, generally speaking. Sometimes the time at which the
callback is called is a bit clearer defined, but it doesn't mean the
path to the callback is (or can be) determined.
I have written some code after reading some tutorials from internet...
But I am not sure is it a right way to write a call back function...
I have mentioned my doubts in code comments.
My code is...

void myfun_callback( void(*fp)(char* )) //is this function is a call
back?
No. This function is not a callback _unless_ you give it to some
other function to be called.
{
(*fp)("Pradeep Kumar");
}

void A(char *msg) //function A,called by myfun_callback
This function is most likely a callback.
{
cout<<"Function A: "<<msg<<end l;
}

void B(char *msg) //function A,called by myfun_callback
This is another function that is likely a callback.
{
cout<<"Function B: "<<msg<<end l;
}

int main()
{
void(*fp)(char* ); //function pointer
fp=A; //giving address of function A
myfun(fp); //right way to call the callback function?
It's a way to call a function that will *in turn* call the
callback you supply to it.
fp=B; //giving address of function B
myfun(fp); //right way to call the callback function?
return 0;
}

ur help will be appreciated..
Thank u in advance
Try to use English here and avoid chatroomisms.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '07 #2
A callback function is a function in your code that is called by
something outside of your control; it could be the operating system,
or it could be a 3rd party library you are using.

This typically means you first give out a pointer to that function to
the external caller.

Other than that, it's a function like any other, but of course, it
should work according to what the external caller expects.

Jun 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2593
by: Marian Aldenhövel | last post by:
Hi, I am using the FMOD audio-library with the pyFMOD python bindings. pyFMOD uses ctypes. It is possible to register callback functions with FMOD that are called at certain points in the processing pipeline or when certain events happen. I am expecially interested in the one that fires when a currently playing stream ends. This is what the declaration in pyFMOD looks like:
4
2054
by: ma740988 | last post by:
// file sltest.h #ifndef SLTEST_H #define SLTEST_H class CallbackBase // herb shutters gotW source .. { public: virtual void operator()() const { }; virtual ~CallbackBase() = 0; };
15
2006
by: Felix Kater | last post by:
Hi, in a given library I register callback functions with this function: bool set_callback(int index, int (*callback_function)(long)); I need the callback function to also pass the index which is, however, not (re-)passed by the callback function. Normally, I would just register callback functions for each
8
572
by: kurtcobain1978 | last post by:
-------------------------------------------------------------------------------- I need to do the exactly same thing in VB.NET. Load a unmanaged C DLL dynamically and then call a function in which I pass the callback function as an argument. My C function being called callback as type _cdecl. Does anybody have any ideas?
6
7681
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in a .NET application and thus am looking into writing a managed C++ wrapper for in vs2005. Furthermore, this library has many callback hooks which need to be implemented by the C++ wrapper. These callback functions are declared as "extern C...
10
6999
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK MyEnumWindowsProc(HWND hwnd, LPARAM lparam) {
0
2114
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via callback.setHandler1(callback1) this only seems to affect pythons ability to trigger an "event" in c. PyObject *Handler is always NULL even after I call Register_Handler(...). I thought there was some magic here that was assigning the pointer *Handler to my python...
6
2163
by: jmDesktop | last post by:
In a function that takes another function (function pointer) as a argument, or the callback function, which is the one that "calls back"? I'm having a hard time understanding the language. Am I right that if function A is: function A(*function pointer to B aka callback func, other arguments) { call (B); //calls }
5
4322
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata); void myfunction (callback_t callback, void *userdata); How do I translate this to C#? I tried with:
0
8818
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9522
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9355
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8234
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4589
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4860
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.