Function Pointers

Get introduced to the function pointers.

Function pointers are for storing addresses of functions to execute those functions at a later time. Function pointers in D are similar to their counterparts in the C programming language.

Delegates store both a function pointer and the context to execute that function pointer in. The stored context can either be the scope that the function execution will occur or a struct or class object.

Delegates also enable closures, a concept that is supported by most functional programming languages.

Function pointers

In the previous chapter, you saw that it is possible to take addresses of functions with the & operator. In one of those examples, we passed such an address to a function template.

Taking advantage of the fact that template type parameters can match any type, let’s pass a function pointer to a template to observe its type by printing its .stringof property:

Get hands-on with 1400+ tech skills courses.