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 having 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
func myFunction1()
{
func myFunction2()
{
print ( "GeeksforGeeks" )
}
myFunction2()
}
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
func add(num1: Int , num2: Int )
{
func printSum(sum: Int )
{
print ( "Sum of 2 and 3 is equal to :" , sum)
}
let totalSum = num1 + num2
printSum(sum : totalSum)
}
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
func add(num1: Int , num2: Int ) -> Int {
func multiplySum(sum : Int ) -> Int {
return 2 * sum
}
let totalSum = num1 + num2
let answer = multiplySum(sum: totalSum)
return answer
}
let result = add(num1 : 8, num2: 3)
print ( "Result:" , result)
|
Output:
Result: 22
Last Updated :
20 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...