English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about function overloading, when you need function overloading, and how to overload examples.
Two or more with the same name but different parametersFunctionsCalled overloaded functions.
Imagine you are developing a shooting game where players can attack enemies with a knife, sword, and gun. Your solution for the attack function may be to define the operation as a function:
func attack() { //.. print("Attack with knife") } func attack() { //.. print("Attack with a sword") } func attack() { //.. print("Attack with gun") }
However, when you try to run the above program, you will receive compilation errors in Swift like 'attack()' previously declared hereHowever, another possible solution may be to define different function names for specific functions, as shown below:
struct Knife { } struct Gun { } struct Blade { } func attackUsingKnife(weapon: Knife) { //.. print("Attack with knife") } func attackUsingBlade(weapon: Blade) { //.. print("Attack with a sword") } func attackUsingGun(weapon: Gun) { //.. print("Attack with gun") }
Don't worry if you don't know what struct is. Now, just think of it as something that creates physical objects in programming, then you are creating a knife, gun, and sword.
The only problem with this solution is that you need to remember the function name to call the specific attack operation. Similarly, as the level increases, players may have other functions to attack using bombs, hand grenades, shotguns, and so on.
Creating functions with different names is time-consuming and increases the overhead of remembering function names to call them. In summary, it is not very intuitive.
If it is possible to create different functions with the same name for each weapon, that would be better. In this way, remembering a single function name is enough, and you don't have to worry about the function names of other weapons.
The process we just described is called function overloading. According to definition, the process of creating two or more functions with the same name but different number or type of parameters is called function overloading.
Let's see this in the following example:
struct Knife { } struct Gun { } struct Blade { } func attack(with weapon: Knife) { print("Attack with knife") } func attack(with weapon: Gun) { print("Attack with gun") } func attack(with weapon: Blade) { print("Attack with a sword") } attack(with: Gun()) attack(with: Blade()) attack(with: Knife())
When you run the above program, the output will be:
Attack with a gun Attack with a sword Attack with a knife
In the above program, we created three functions with the same name but different parameter types named attack. This way, you can call the function by remembering the name attack.
Calling attack(with: Gun()) will trigger the func attack(with weapon: Gun) statement inside the function.
Calling attack(with: Blade()) will trigger the func attack(with weapon: Blade) statement inside the function.
Calling attack(with: Knife()) will trigger the func attack(with weapon: Knife) statement inside the function.
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")
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 have two functions with the same name and the same number of parameters named output(). However, the first output() function takes an integer as a parameter, and the second output() function takes a String parameter.
compared to the example1Similarly,
Calling output(x: 2) will trigger the func output(x: Int) statement inside the function
Calling output(x: "Swift") will trigger the func output(x: String) statement inside the function.
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)
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 overloaded based on the number of parameters.
The first output() without parameters, the second output() with one parameter: String, and the third output() with two parameters: String and Int.
Let's try to overload by changing the parameter names, but keeping the parameter labels unchanged, as shown below:
func output(value text: String) { print(text) } func output(value num: Int) { print(num) } output(value: 2) output(value: "Hello")
When you run the above program, the output will be:
2 Hello
As you can see in the above program, you can use the same parameter labels for overloaded functions. However, according to the requirements of overloading, you must have a different number of parameters or different types of parameters.