Bifalgorithm post · Computer science one · Function Pointers

How do callbacks make C functions more generic?

How do callbacks make C functions more generic? (Computer science one · Function Pointers) — "The main use for function pointers is so that references to funct…

Ideas in this flowchart

  • Function pointers enable callbacks and generic function design.
  • Function references can be passed as parameters to other functions.
  • The function passed as a parameter is called a callback.
  • Callbacks allow functions to be written in a more abstract and generic way.
  • GUI programming uses callbacks to associate functions with events.
  • A button registration function associates the click event with a provided callback.
  • When the user clicks the button, the associated callback is invoked.
  • A getMax() function illustrates replacing specialized implementations with a generic design.
  • Without callbacks, separate getMax() functions would be needed for integers, doubles, students, GPAs, IDs, and names.
  • The non-generic version operates on an integer array and tracks the index of the largest value.
  • The integer implementation uses the less-than operator to update the maximum index when a larger value is found.
  • The generic version accepts a void array and a comparator callback to replace the less-than comparison.
  • The generic getMax() signature receives the array, its length, and a comparator function pointer.
Feed/

How do callbacks make C functions more generic?

No cover

Channel

Computer science one

1 page

"The main use for function pointers is so that references to functions can be passed as parameters to other functions. The passed function is known as a callback. This gives us the ability to write a more abstract and generic function. For example, in GUI programming, we frequently need to associate a particular function with a particular event. Suppose we create a button; we need to be able to specify what happens when that button gets clicked. We do so by providing a function as a callback to a registration function that associates the “click” event with the provided function. Thus, whenever a user clicks the button, the callback is invoked (“called back”).\n\nLet’s illustrate some syntax usage with another example. Suppose we want to create a getMax() function. We could write one function for arrays of integers, another for arrays of doubles, another for Student arrays that gets the student with the maximum GPA, then another for the ID, then another for the name and so on. Or, we could program a generic getMax() function that could be used for any type by taking a comparator function as a callback. To illustrate, consider the following non-generic function for integers.\n\nint getMax(const int *arr, int n) {\n\nint i, maxIndex = 0;\n\nfor(i=1; i<n; i++) {\n\nif(arr[maxIndex} < arr[i] {\n\n//we've found something larger, update the maxIndex:\n\nmaxIndex = i;\n\n}\n\n}\n\nreturn maxIndex;\n\n}\n\nThis simple function iterates through the array, keeping track of the maximum value found “so far” and updating it when it finds something larger. Because we’ve specified that arr is an array if int values, we can use the less-than comparison operator (line 4).\n\nNow let’s make it more generic: rather than taking an array of int values, it will now take a generic void array. Further, we will pass a function pointer to this function that references a generic comparator function that can be used to replace the less-than comparison operator.\n\nint getMax(const void *arr, int n,\n\nint(*cmp)(const void *, const void *)) {\n\nint i, maxIndex = 0;\n\nfor(i=1; i<n; i++) {\n\n...\n\n}\n\nreturn maxIndex;\n\n}"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.