Higher-order function

Or in a tacit manner: Using std::function in C++11: Or, with generic lambdas provided by C++14: Using just delegates: Or equivalently, with static methods: In Elixir, you can mix module definitions and anonymous functions Alternatively, we can also compose using pure anonymous functions.

Notice a function literal can be defined either with an identifier (twice) or anonymously (assigned to variable plusThree).

E.g., the function g could be implemented equivalently: In Raku, all code objects are closures and therefore can reference inner "lexical" variables from an outer scope because the lexical variable is "closed" inside of the function.

Raku also supports "pointy block" syntax for lambda expressions which can be assigned to a variable or invoked anonymously.

However, macros cannot easily avoid the problem of variable capture; they may also result in large amounts of duplicated code, which can be more difficult for a compiler to optimize.

In other imperative programming languages, it is possible to achieve some of the same algorithmic results as are obtained via higher-order functions by dynamically executing code (sometimes called Eval or Execute operations) in the scope of evaluation.

There can be significant drawbacks to this approach: In object-oriented programming languages that do not support higher-order functions, objects can be an effective substitute.

Objects often carry added run-time overhead compared to pure functions, however, and added boilerplate code for defining and instantiating an object and its method(s).

Languages that permit stack-based (versus heap-based) objects or structs can provide more flexibility with this method.