What is a callback function?

前端 未结 21 2459
耶瑟儿~
耶瑟儿~ 2020-11-21 23:01

What is a callback function?

21条回答
  •  遥遥无期
    2020-11-21 23:41

    Assume we have a function sort(int *arraytobesorted,void (*algorithmchosen)(void)) where it can accept a function pointer as its argument which can be used at some point in sort()'s implementation . Then , here the code that is being addressed by the function pointer algorithmchosen is called as callback function .

    And see the advantage is that we can choose any algorithm like:

      1.    algorithmchosen = bubblesort
      2.    algorithmchosen = heapsort
      3.    algorithmchosen = mergesort   ...
    

    Which were, say,have been implemented with the prototype:

      1.   `void bubblesort(void)`
      2.   `void heapsort(void)`
      3.   `void mergesort(void)`   ...
    

    This is a concept used in achieving Polymorphism in Object Oriented Programming

提交回复
热议问题