Swift Function
A function is a set of instructions clubbed together to perform a specific task. In Swift, we can define a function using the func keyword. You can also set the return type by specifying the return type followed by a return arrow(“->”).
For example,

Swift Function Overloading
In Swift, if two or more functions are defined using the same name with a different set of parameters(different types of parameters, different number of parameters or both), this is known as the Function Overloading. These functions are known as the Overloaded functions.
Function overloading makes us possible to create some functions with the same name having a different number of parameters or different types of parameters.
For example,

The function, namely func_name, is overloaded using different number of parameters.
Examples
The examples of function overloading are as follows.
Example1(Overloading with Different Types of Parameters)
Here in this example, we will see how to overload a function with different types of parameters.
Implementation
func print_value(value: Int){
print("Integer Value: ", value)
}
func print_value(value: Double){
print("Double Value: ", value)
}
// it calls the first print_value function
print_value(value: 1)
//it calls the second print_value function
print_value(value: 1.68)
Output

Example2(Overloading with Different Number of Parameters)
Here in this example, we will see how to overload a function with different number of parameters.
Implementation
func sum(value1: Int, value2: Int){
print("The sum of two number is: ", value1 + value2)
}
func sum(value1: Int, value2:Int, value3: Int) {
print("The sum of three number is: ", value1 + value2 + value3);
}
//it calls the first function
sum(value1: 1 , value2: 2)
//it calls the second function
sum(value1: 1 , value2: 2 , value3: 3)
Output

Frequently Asked Questions
What is Swift in iOS Development?
Swift is a robust, intuitive and powerful open language created by Apple that is used to build powerful apps for IOS, MAC, Apple Watch and Apple TV.
Is iOS written in Swift?
Most modern iOS apps are written and developed using Swift.
What languages does Swift use?
Swift has taken references from programming languages including Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and a few more that can be added to the list.
What are the best uses of the Swift?
The best uses of the Swift include building apps for iOS, Mac, Apple TV and Apple Watch.
Conclusion
In this article, we have extensively discussed the Function Overloading in Swift.
We started with the basic introduction, then we discussed,
- What Swift is
- What is the function in Swift
- Function Overloading in Swift
-
Lastly, we gave two examples of function overloading.
We hope that this blog has helped you enhance your knowledge regarding Function Overloading in Swift and if you would like to learn more, check out our articles on The Definitive Swift Tutorial. Do upvote our blog to help other ninjas grow.
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations and much more.!
Happy Reading!