A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 4 years ago

please xplain me the advantage of callback functions,terminology used ..suppose a function takes argument as pointer to function then which function is called callback..? exg: fun(*ptr_to_anthrfunction) and in code instead of executing this *ptr_to_anthrfunction i could have called directly anthrfunction().... need a brief explaination regarding terminology too...

  • This Question is Closed
  1. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    If you pass in a pointer to a function to call, you can replace this function with a different one without the callee needing to know. Imagine for example an elaborate sort function that operates on an array of structures: struct myStruct { int val_one; int val_two; }; You could write two separate sort functions that sort the array by val_one and val_two, respectively, duplicating lots of the sorting code itself. Or, you could implement two tiny compare functions: bool aSmallerThanB_valone(myStruct *a, myStruct *b) { return a->val_one < b->val_one; } bool aSmallerThanB_valtwo(myStruct *a, myStruct *b) { return a->val_two < b->val_two; } And pass a pointer to the compare function into your sort function. That way you're not reimplementing any code unnecessarily, and can add additional potential sort keys to myStruct by just implementing a one-line compare function, instead of reimplementing the entire sort function.

  2. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks opiesche... can you please explain little more elaborate on your sentence "f you pass in a pointer to a function to call, you can replace this function with a different one without the callee needing to know" and how does synchronous and asynchronous functions come into picture...in callback functions

  3. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Ah, I misunderstood the question. So, in the sort example above, you've got a single sort function that, for example, implements a quicksort. Every sort function needs a comparison operation so it knows how to sort two elements of the array - in the example above, instead of implementing the comparator directly in the sort function, you can implement it as a separate function that is then used by the sort by passing it in as a function pointer. That means that the sort function doesn't have to care or know about what the comparator function does - only that it returns true or false for the comparison of two elements. You could have an array of RGB colors, and by just writing a simple comparator function, *the same quicksort function* that sorts an array of ints by size, for example, you could have the color array sorted by luminance. This makes the sort function independent of the comparison operation, and hence the type of data in the array, in our example. That's for function pointers in general. A callback is a more specific case for the use of a function pointer. Say you've got an expensive operation ExpensiveFunc that you're running asynchronously in a separate thread. While that operation is running, you still want to display a progress bar. You could pass a pointer to your UpdateProgressBar function to ExpensiveFunc, and ExpensiveFunc could periodically call that function to update the progress value, so that the progress is displayed to the user.

  4. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.