language concepts - What's the point behind passing functions as arguments to other functions? -


it's rather general purpose question , not specific 1 language. don't quite understand point behind passing function argument function. understand if function, say, foo1() needs use result returned function foo2(), why can't values returned/updated foo2() passed foo1() is? or in scenario, why can't foo2() called within foo1() results being used therein? happens under hood when foo2() passed argument foo1()? foo2() executed prior foo1()?

generally speaking, pass function foo2 function foo1 in cases multiple evaluations of foo2 necessary - , perhaps don't know in advance parameters used each call of foo2, couldn't possibly perform calls in advance.

i think sort() function/method on lists might best concrete example. consider list of people - might reasonably want sort them alphabetically name, or numerically age, or geographical distance given point, or many other possible orders. hardly practical include every such ordering built-in options sort(): usual approach taken languages allow caller provide function parameter, defines ordering between items of list.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -