Programming on iOS with Swift: Working with Functions and Nested Functions (Beginners)

Nested Functions

An under-utiilzed but interesting use of functions are as nested functions, functions that are essentially not global but privately embedded within another function. Sure you can have functions declared as private, but that will only protected the function from being accessed by another class. 

Protected from external access, nested functions can be called and used by their enclosing function, which can also return the nested function. I see nested functions as providing an encapsulated convenience like closures, allowing you to complete your function within the function rather than chasing the calls across the class. In the following code, the nested function is only accessible within it’s parent or enclosing function:

Author: Doron Katz

Hi, I’m Doron, a data-driven technical program manager, published author, proud father and husband, based in Seattle.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s