473,804 Members | 2,223 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 2638
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
2599
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
2055
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
2012
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
7685
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
7001
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
2119
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
2165
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
4323
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
9594
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,...
1
10341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10089
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9171
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
7634
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
5530
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.