Swift Nested Functions

In Swift, a function can be defined inside another function. Such functions are called nested functions. A nested function has access to the variables, constants, and other functions declared in its enclosing function.

To define a nested function, we declare it inside the body of the enclosing function. Here's the syntax:

1func outerFunction() { 2 3 // defining a nested function 4 func innerFunction() { 5 // function body 6 } 7 8 // calling the nested function 9 innerFunction() 10}

In the above example, we have defined a nested function innerFunction() inside the body of the enclosing function outerFunction(). We can call the nested function innerFunction() from within the enclosing function outerFunction().

Accessing Variables in Enclosing Function

As mentioned earlier, a nested function can access the variables, constants, and other functions declared in its enclosing function. Here's an example:

1func outerFunction() { 2 let a = 10 3 func innerFunction() { 4 print("The value of a is \(a)") 5 } 6 innerFunction() 7} 8outerFunction() // Output: The value of a is 10

In the above example, we have defined a nested function innerFunction() that accesses the variable a declared in its enclosing function outerFunction().

Benefits of Nested Functions

There are several benefits of using nested functions in Swift:

  1. Encapsulation: By defining a function inside another function, we can encapsulate related functionality in one place, which can make the code easier to understand and maintain.

  2. Code Reusability: Since a nested function can access the variables and other functions declared in its enclosing function, we can reuse the same functionality in different parts of the enclosing function.

  3. Namespace Control: By defining a function inside another function, we can limit the scope of the function to only the enclosing function, which can prevent naming conflicts with functions declared elsewhere.

Example: Calculating the Sum of Even Numbers

Let's look at an example of how we can use nested functions to calculate the sum of even numbers in an array:

1func calculateSumOfEvenNumbers(_ numbers: [Int]) -> Int { 2 var sum = 0 3 4 func isEven(_ number: Int) -> Bool { 5 return number % 2 == 0 6 } 7 8 func addNumberToSum(_ number: Int) { 9 sum += number 10 } 11 12 for number in numbers { 13 if isEven(number) { 14 addNumberToSum(number) 15 } 16 } 17 18 return sum 19} 20 21let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 22let sumOfEvenNumbers = calculateSumOfEvenNumbers(numbers) 23print("The sum of even numbers in the array is: \(sumOfEvenNumbers)")

In the above example, we have defined three nested functions:

  • isEven(_:): This function takes an integer as input and returns true if the integer is even.
  • addNumberToSum(_:): This function takes an integer as input and adds it to the sum of even numbers if it is even.
  • calculateSumOfEvenNumbers(_:): This is the main function that takes an array of integers as input, loops through the array, and calls the isEven(_:) and addNumberToSum(_:) functions as needed.

When we run the code, the output will be: The sum of even numbers in the array is: 30