Function Overloading in Swift


In this tutorial, we are going to learn about function overloading in Swift with some examples.

In Swift programming, it is possible to have the same name for two or more functions if these functions differ in parameter. These functions may have a different number of parameters or different types of parameters or both.

These functions with the same name are called overloaded functions and this phenomenon is known as function overloading.

Below is an example of function overloading:

Below is an example:

// Same function names, but with different arguments
func myFunction() {
    // ....
}
func myFunction(value: Int) -> Int{
    // ....
}
func myFunction(value: String) -> String{
    // ....
}
func myFunction(value1: Int, value2: Double) -> Double{
    // ....
}

In the above example, we have four functions with the same function name myFunction. But you can notice that each one has different arguments or parameters. This is how function overloading works.

Well, the above example will show errors if you try to run the code. This is because there are no return values in functions. The example is just to demonstrate how Swift function overloading works.

Now let’s see some real examples of function overloading:

Function overloading with different types of parameters

// function with Integer type parameter
func myFunction(value: Int) {
    print(value)
}

// function with String type parameter
func myFunction(value: String) {
    print(value)
}

// call the function with String type parameter
myFunction(value: "SwiftSpeedy")

// call the function with Int type parameter
myFunction(value: 7)

Output:

SwiftSpeedy
7

In our above Swift code example, we have overloaded the function myFunction(). The first one has integer-type parameters. On the other hand, the second function has a parameter of string type. Thus, these two parameters differ in parameters.

Function overloading with differences in the number of parameters

// the function has two parameters
func myFunction(value1: Int, value2: Int) {
    print("1st Int value: \(value1) and 2nd Int value: \(value2)")
}

// the function has one parameter
func myFunction(value1: Int) {
    print("Integer value: \(value1)")
}

// call the function with one parameter
myFunction(value1: 17)

// call the function with two parameters
myFunction(value1: 8, value2: 13)

Output:

Integer value: 17
1st Int value: 8 and 2nd Int value: 13

This time, we have the same type of parameters, but there are differences in the total number of parameters. In this way, it is also making a difference and thus the function myFunction() becomes overloaded.

Function overloading with Argument Label

func myFunction(student1 name:String) {
    print("Person1 Age:", name)
}

func myFunction(student2 name:String) {
    print("Person2 Age:", name)
}

myFunction(student1: "Emily")
myFunction(student2: "Itachi")

Output:

Person1 Age: Emily
Person2 Age: Itachi

In this example, we have different argument levels with the same function name. Two or more functions with different argument levels also can be overloaded. Thus, this time also, the function myFunction() is overloaded.