- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
Swift - Function Overloading
A Function is a snippet of code that is used to perform a specific task. In Swift, we are allowed to overload functions. Function overloading is a technique in which we can create multiple same-name functions but with different parameters or argument labels.
So when an overloaded function is called, the compiler determines which function should execute according to the number of parameters and types of parameters provided at the time of call. In Swift, we cannot overload functions according to the return type.
We can overload functions in the following ways −
Overloading with a different number of parameters.
Overloading with different parameter types.
Overloading with argument label.
Overloading with Different Number of Parameters
In function overloading with a different number of parameters, the names of the two or more functions are the same but the number of parameters is different. The functions can have the same or different names of parameters. In this type of function overloading, the function is invoked according to the number of parameters provided by the user during the function call.
Example
Swift program to demonstrate function overloading with a different number of parameters.
import Foundation // Function 1 func product(num1: Int, num2: Int){ // Calculating product let result = num1 * num2 print("Function 1: Product = ", result) } // Function 2 func product(num1: Int, num2: Int, num3: Int){ // Calculating product let result = num1 * num2 * num3 print("Function 2: Product = ", result) } // Function 3 func product(num1: Int, num2: Int, num3: Int, num4: Int){ // Calculating product let result = num1 * num2 * num3 * num4 print("Function 3: Product = ", result) } // Calling function 1 product(num1: 23, num2: 34) // Calling function 2 product(num1: 53, num2: 34, num3: 34) // Calling function 3 product(num1: 23, num2: 34, num3: 55, num4: 21)
Output
It will produce the following output −
Function 1: Product = 782 Function 2: Product = 61268 Function 3: Product = 903210
Overloading with Different Parameter Types
A function can be overloaded with different parameter types which means multiple functions contain the same name and number of parameters but the parameter types are different. In this type of function overloading, the function is invoked according to the type of parameters provided by the user during the function call.
Example
Swift program to add two strings using nested function.
import Foundation // Function 1 func Addition(num1: Int, num2: Int){ // Adding two values let result = num1 + num2 print("Function 1: Result = ", result) } // Function 2 func Addition(num1: String, num2: String){ // Adding two values let result = num1 + num2 print("Function 2: Result = ", result) } // Calling function 1 Addition(num1: 23, num2: 34) // Calling function 2 Addition(num1: "Hello!", num2: "Tutorialspoint")
Output
It will produce the following output −
Function 1: Result = 57 Function 2: Result = Hello!Tutorialspoint
Overloading with Argument Label
We can overload functions according to their argument labels. In such type of overloading, the function names are the same but the argument label is different. So that at the time of execution compiler can easily identify which function should it invoke.
Example
Swift program to overload function according to their argument labels.
import Foundation // Function 1 func Area(length: Int, width: Int){ let result = length * width print("Function 1: Result=", result) } // Function 2 func Area(l: Int, w: Int){ let result = l * w print("Function 2: Result=", result) } // Calling function 1 Area(length: 23, width: 3) // Calling function 2 Area(l:13, w: 3)
Output
It will produce the following output −
Function 1: Result= 69 Function 2: Result= 39
Advantages of Function Overloading in Swift
The following are the advantages of function overloading −
It improves the code readability.
Reduce multiple function names by giving the same name to all the functions.
It enhances type safety and reduces runtime errors.