Nested Function in Swift


In this programming tutorial of Swift, you are going to learn about the nested function with examples.

To follow this tutorial, you should learn about Swift functions first. If you already know what functions are, then you can continue learning from this tutorial…

What is a Nested function in Swift programming?

A nested function is a function that is in the body of a parent function.

Swift allows us to create a function inside the body of another function. So if we create a function inside another function, then the inner function is called a nested function.

A nested function can be called only inside its outer function. For example, if a nested function func2 is inside another function func1, then the function func2 can be only called inside the func1.

It is possible to set parameters, argument labels, set return type in the Swift nested functions also just like you can do with regular functions.

Syntax for nested function

Below is the given syntax for creating a nested function:

// parent function
func func1() {
  // some code in outer function...

  // inner function
  func func2() {
    // some code in inner function...
  }

}

In the above syntax, the inner function func2 is the nested function. This is because func2 is inside an outer function func1.

Creating a nested function

Now it’s time to create our first nested function in Swift…

// outer function
func helloMessage() {

  // inner function inside the outer function
  func displayMessage() {
    print("Hello Everyone!")
  }

  // calling inner function inside the outer function
  displayMessage()
}

// calling outer function
helloMessage()

The output of the above code is:

Hello Everyone!

Remember, we can’t call the inner function outside of the parent function.

You can see from the example, it is just like creating a regular function. The only difference is that it is inside another function.

Nested functions with parameters

Now see another example of a nested function with a parameter:

func helloMessage() {

  func displayMessage(name: String) {
    print("Hello "+name)
  }

  displayMessage(name: "OLIVIA")
}

helloMessage()

Output:

Hello OLIVIA

The above example only has one parameter in the inner function. Now below is another example with multiple parameters in the nested function:

func helloMessage() {

  func displayMessage(name1: String,name2: String) {
    print("Hello "+name1+" and "+name2)
  }

  displayMessage(name1: "OLIVIA", name2: "JOSHUA")
}

helloMessage()

Output:

Hello OLIVIA and JOSHUA

Nested function with return type

We can also return the value of a specific type from the nested function just like we do in the case of normal functions. Below is the example:

func helloMessage() {

  func displayMessage(name1: String,name2: String) -> String {
    return "Hello "+name1+" and "+name2
  }

  print( displayMessage(name1: "OLIVIA", name2: "JOSHUA") )
}

helloMessage()

Output:

Hello OLIVIA and JOSHUA

In the above example, we have returned a string type value.

Argument labels in nested functions

Argument labels are used to make it looks like a sentence-like statement that makes sense.

In the example below, we have set the argument labels for the inner functions:

func outerFunction() {
    func sum(of num1: Int, and num2: Int) {
      let sum = num1 + num2
      print(sum)
    }

    sum(of: 4, and: 3)
}

outerFunction()

Output:

7

In the above example, we have created an inner function inside a parent function to calculate the sum of two numbers.