O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter    28

Nested Functions

You can code functions within other functions. Nesting functions gives you a way to organize and reuse your code while limiting the scope of functions to the parent function.

For instance, let’s say you wanted to write a Swift program to help a teacher summarize how some students performed on the tests they took this week. You could write a function called analyzeTestScores() that would figure out what the average score of a test was including all students and then would write a report.

Since this function has to do a few things, it makes sense to organize each task into separate functions. You could start by adding a function that returns the average test score based on an array of numbers (see Listing 28-1). ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required