First-class and higher-order functions
In functional programming, functions are regarded as first-class objects (but you may also come across as first-class citizens). This implies that we should handle them as objects as opposed to a set of instructions. What difference does this make to us? The only criterion for a function to be considered an object at this point is its ability to be passed to other functions. Higher-order functions are defined as functions that accept other functions as arguments.
Programmers in C++ frequently pass one function to another. Here’s how to do it the old-school way:
typedef void (*PF)(int);void foo(int arg) { Â Â Â Â // do something with arg } int bar(int arg, PF f) { Â Â Â Â f(arg); Â Â Â Â return arg; } bar(42, foo);
We declared a pointer to a function in the code that was written here. With one integer parameter and no value returned, PF
denotes a type definition for the function. This...