Swift Function Overloading: In this tutorial, you will learn about function overloading, when do we need function overloading and how to overload with examples.

At the point when two are more functions have same name but different arguments then they are known as overloaded functions and this process in known as function overloading.

Two or more functions having same name however unique argument(s) are known as overloaded functions.

Why do we need function overloading?

Imagine you are building up a shooter game where the player can attack its adversaries using a knife a blade and a weapon. Your solution for the attack usefulness may be defining the activities into functions as:

func attack() {
    //..
    print("Attacking with Knife")
}

func attack() {
    //..
    print("Attacking with Blade")
}

func attack() {
    //..
    print("Attacking with Gun")
}

However, when you attempt to run the above program, you will get a compile time error in Swift as ‘attack()’ previously declared here. Nonetheless, another arrangement may be defining different function names for the specific usefulness as:

struct Knife {
}
struct Gun {
}
struct Blade {
}

func attackUsingKnife(weapon:Knife) {
    //..
    print("Attacking with Knife")
}

func attackUsingBlade(weapon:Blade) {
    //..
    print("Attacking with Blade")
}

func attackUsingGun(weapon:Gun) {
    //..
    print("Attacking with Gun")
}

Try not to stress on the off chance that you don’t know what struct is. Until further notice simply consider it something that creates a physical object in programming, so you’re creating a knife, weapon and blade. On the off chance that you need to know more, see Swift Struct. If not, we’ll return to it in the later chapters.

The only issue with this solution is you need to recall the function names to call that specific attack activity. Likewise as the level rises, the player may have extra highlights for attack using bomb, grenade, shotgun, and so on

Creating function with various names is time consuming and increases overhead of recalling the function name to call it. All things considered, it’s not intuitive.

It would be vastly improved in the event that you could create various functions with a similar name however unique execution for every weapon. Along these lines, recollecting that one function name is sufficient and you wouldn’t need to stress over the function names for different weapons.


What is function overloading?

The interaction we just depicted is known as function overloading. By definition, the way toward creating two or more than two functions with a similar name however having different number or sorts of parameters passed is known as function overloading.

Let’s see this in below example:

Example 1: Function Overloading

struct Knife {
}
struct Gun {
}
struct Blade {
}

func attack(with weapon:Knife) {
    print("Attacking with Knife")
}

func attack(with weapon:Gun) {
    print("Attacking with Gun")
}

func attack(with weapon:Blade) {
    print("Attacking with Blade")
}

attack(with: Gun())
attack(with: Blade())
attack(with: Knife())

At the point when you run the above program, the output will be:

Attacking with Gun
Attacking with Blade
Attacking with Knife

In the above program, we have created three different functions with a similar name attack. Be that as it may, it acknowledges different parameter types. This way recalling attack name to call the function is sufficient.

  • The call attack(with: Gun()) triggers the statement inside the function func attack(with weapon:Gun).
  • The call attack(with: Blade()) triggers the statement inside the function func attack(with weapon:Blade).
  • The call attack(with: Knife()) statement inside the function func attack(with weapon:Knife).

Example 2: Function Overloading based on different parameter types

func output(x:Int) {
    print("The int value is \(x)")
}

func output(x:String) {
    print("The string value is \(x)")
}

output(x: 2)
output(x: "Swift")

At the point when you run the above program, the output will be:

The int value is 2
The string value is Swift

In the above program, we’ve two functions with a similar name output() and same number of parameters. Nonetheless, the first output() function takes an integer as a parameter, and the subsequent output() work takes String as a parameter.

Similar to the Example 1,

  • the call to output(x: 2) triggers the statement inside the function func output(x:Int) and
  • the call to output(x: “Swift”) triggers the statement inside the function func output(x:String).

Example 3: Function Overloading based on different number of parameters

func output() {
    print("Good Morning!")
}

func output(text:String) {
    print(text)
}

func output(text:String, num:Int) {
    print("\(text)\(num)!")
}

output()
output(text: "Good Evening!")
output(text1: "Good N", num: 8)

At the point when you run the above program, the output will be:

Good Morning!
Good Evening!
Good Night!

In the above program, the function output() has been overloading dependent on the number of arguments.

The initially output() takes no parameters, the subsequent output() takes a single parameter: String, and the third output() takes two parameters: String and Int.

Lets attempt to overload by changing the parameter name however keeping the argument label same as:


Example 4: Function overloading with same argument label

func output(value text:String) {
    print(text)
}

func output(value num:Int) {
    print(num)
}

output(value: 2)
output(value: "Hello")

At the point when you run the above program, the output will be:

2
Hello

As should be obvious, in the above program, you can use a similar argument label for the overloaded functions. In any case, as overloading requires, you should have either different number of parameters or various types of parameters.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.


0 Comments

Leave a Reply

Avatar placeholder