Table of contents
1.
Introduction
2.
About Swift
3.
Swift Function
4.
Swift Function Overloading
5.
Examples
5.1.
Example1(Overloading with Different Types of Parameters)
5.1.1.
Implementation
5.1.2.
Output
5.2.
Example2(Overloading with Different Number of Parameters)
5.2.1.
Implementation
5.2.2.
Output
6.
Frequently Asked Questions
6.1.
What is Swift in iOS Development?
6.2.
Is iOS written in Swift?
6.3.
What languages does Swift use?
6.4.
What are the best uses of the Swift?
7.
Conclusion
Last Updated: Mar 27, 2024

Function Overloading in Swift

Author Aniket Majhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! We hope that you are doing well. 

A recent report states that the iOS industry is diminishing due to Android's increasing popularity and robustness. A recent report indicates that iOS is being used by 26.28% of mobile users worldwide, up from 22.71% in 2019, making it the second most popular mobile platform

The iOS industry has gained momentum after the ingress of the Swift. Many businesses are moving their tech stack to Swift, which increased the demand for Swift Developers.

Today, we will be discussing the Function Overloading in Swift. If you want to become one of Swift Developers and want to make an impact in this field, follow this article till the end.

If you want to learn more about Swift, you can refer to The Definitive Swift Tutorial article.

So, without further ado, let's start the topic.

                               Source

About Swift

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. Swift is easy to use and open source. You can easily implement your idea in Swift and create something incredible.

Swift Function

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 TutorialDo 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!

Live masterclass