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...
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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:
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.
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
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.