Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Swift – Nested Function

  • Last Updated : 20 Dec, 2021

A function is a collection of statements grouped together as a block to perform a specific task. The return type of a function decides the type of value returned by the function. For example, if we want to get an integer value from a function then the return type of the function must be Int, if we want to get decimal value from a function then the return type must be either Float or Double etc. A function in Swift may or may not contain any return type. In this article, we will learn nested functions.

Nested Functions

In Swift, when a function exists inside another function then such types of functions are known as nested functions. The nested function is hidden inside the outer function or enclosed and cannot be accessed by the outside functions if we try to access them we will error. They can only be called and be used by their enclosed function.

Syntax:

func myFunction1

{

    // body of myFunction1

    func myFunction2

    {

        // body of myFunction2

    }

}

Here, myFunction1 is the outer function and myFunction2 is the inner function.

Nested Function Having No Return Type

Like a normal function, a nested function may not contain any return type. The working of a nested function

ing no return type is explained below.

Example:

In the below program, we have called the outer function, “myFunction1()” from outside the outer function. Once this function is called, the flow of control of the program goes inside the “myFunction1()” and reaches the statement in which we are calling the function, “myFunction2()”. Now, this function would be called which prints a string using a print() function. After executing this statement the flow of control of the program comes out of the inner function then the outer function and so on.

Swift




// Swift program to demonstrate the working 
// of nested functions having no return type
  
// Outer function having another function 
// to print a string
func myFunction1()
{
  
    // Inner function having print statement
    func myFunction2() 
    {
        print("GeeksforGeeks")
    }
      
    // Calling inner function inside of
    // the outer function 
    myFunction2()
}
  
// Driver code
  
// Calling outer function outside 
// of the other function
myFunction1()

Output:

GeeksforGeeks

Nested Function Having Parameters

Like a function, a nested function may contain parameters also. The working of a nested function having parameters is explained below.

Example:

In the below program, we have called the outer function, “add()” from outside the outer function by passing two parameters num1: 2 and num2: 3 as parameters. Once this function is called, the flow of control of the program goes inside the “add()” function and it reaches the statement in which we are computing the sum of the two integers. Now, the “totalSum” is passed to the inner function, “printSum()”  in which we are printing the value represented by the variable totalSum by using a print function.

Swift




// Swift program to demonstrate the working 
// of nested function having parameters
  
// Outer function to add two integers
func add(num1: Int, num2: Int)
{
  
    // Inner function to print the desired sum
    func printSum(sum: Int
    {
      
        // Print statement
        print("Sum of 2 and 3 is equal to :", sum)
    }
      
    // Calling inner function inside of 
    // the outer function 
    let totalSum = num1 + num2
      
    // Calling inner function
    printSum(sum : totalSum)
}
  
// Driver code
  
// Calling outer function outside of
// the other function
add(num1: 2, num2: 3)

Output:

Sum of 2 and 3 is equal to : 5

Nested Function Having a Return Type

Again, like a function, a nested function may contain a return type. The working of a nested function having a return type is explained below.

Example: 

In the below program, we have called the outer function, “add()” from outside the outer function by passing two parameters num1: 8 and num2: 3 as parameters. Note that the return type of this function is Int. Once this function is called, the flow of control of the program goes inside the “add()” function and it reaches the statement in which we are computing the sum of the two integers. Now, “totalSum” is passed to the inner function, “multiplySum()” whose return type is also Int. In this function, the sum is being multiplied by two and then the value has been returned to the function calling statement. The final answer was stored in the variable, answer inside the outer function. 

Swift




// Swift program to demonstrate the working of
// swift nested functions having return types
  
// Outer Function to add two integers
// Inside this function we are calling 
// inner function. It multiplies the 
// sum by two and returns the value
func add(num1: Int, num2: Int) -> Int{
      
    // Inner function
      func multiplySum(sum : Int) -> Int{
            
          // Return the result from the inner function
          return 2 * sum
      }
           
      // Calculates total sum
      let totalSum = num1 + num2
        
      // Calculate the final answer by calling
      // inner function
    let answer = multiplySum(sum: totalSum)
      
    // Return the final answer from the outer function
    return answer
}
  
// Driver code
  
// Store the answer returned by add function
let result = add(num1 : 8, num2: 3)
  
// Print the answer
print("Result:", result)

Output:

Result: 22

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!