Nested Functions, Structs, and Classes

Understand what nested functions, structs and classes are.

Nested functions

Up to this point, we have been defining functions, structs, and classes in the outermost scopes (i.e., the module scope). They can be defined in inner scopes as well. Defining them in inner scopes helps with encapsulation by narrowing the visibility of their symbols, and creating closures, which we covered in the function pointers, delegates, and lambdas chapter.

As an example, the following outerFunc() function contains definitions of a nested function, a nested struct, and a nested class:

Get hands-on with 1200+ tech skills courses.