Swift –函数重载
在 Swift 中,函数是一组组合在一起以执行特定任务的语句。它是使用“func”关键字声明的。我们还可以通过指定数据类型后跟返回箭头(“->”)来设置函数的返回类型。
句法:
func functionName(parameters) -> returnType {
// body
}
例子:
在下面的程序中,我们通过定义一个既不包含任何参数也不包含返回类型的函数来打印字符串。
Swift
// Swift program to illustrate how we
// can print a string using a function
// Function to print a string
// This function have no return type
func print()
{
print("GeeksforGeeks")
}
// Calling print function
print()
Swift
// Swift program to illustrate function overloading
// Function 1 to calculate sum
func printSum(value1: Int, value2: Int)
{
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
print("The sum of", value1, "and",
value2, "is equal to", sum)
}
// Function 2 to calculate sum
func printSum(value1: Int, value2: Int, value3: Int)
{
// Calculating sum of value1 and value2
let sum = value1 + value2 + value3
// Print the sum
print("The sum of", value1, ",",
value2, "and", value3,
"is equal to", sum)
}
// Function 3 to calculate sum
func printSum(value1: Int, value2: Int, value3: Int,
value4: Int)
{
// Calculating sum of value1 and value2
let sum = value1 + value2 + value3 + value4
// Print the sum
print("The sum of", value1, ",",
value2, ",", value3, "and",
value4, "is equal to", sum)
}
// Calling printSum() function to calculate sum
printSum(value1: 5, value2: 15)
// Calling printSum() function to calculate sum
printSum(value1: 5, value2: 15, value3: 25)
// Calling printSum() function to calculate sum
printSum(value1: 5, value2: 15, value3: 25, value4: 35)
Swift
// Swift program to illustrate function overloading
// Function 1 to calculate sum
func printSum(value1: Int, value2: Int)
{
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
print("The sum of", value1, "and",
value2, "is equal to", sum)
}
// Function 2 to calculate sum
func printSum(value1: Int, value2: Float)
{
// Calculating sum of value1 and value2
let sum = Float(value1) + value2
// Print the sum
print("The sum of", value1, "and",
value2, "is equal to", sum)
}
// Function 3 to calculate sum
func printSum(value1: Float, value2: Float)
{
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
print("The sum of", value1, "and",
value2, "is equal to", sum)
}
// Calling printSum() function to calculate sum
printSum(value1: 5, value2: 15)
// Calling printSum() function to calculate sum
printSum(value1: 5, value2: 15.123)
// Calling printSum() function to calculate sum
printSum(value1: 5.123, value2: 15.123)
Swift
// Swift program to illustrate function overloading
// Function 1 to calculate sum
func printSum(value1: Int, value2: Int)
{
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
print("The sum of", value1, "and",
value2, "is equal to", sum)
}
// Function 2 to calculate sum
func printSum(variable1: Int, variable2:Int)
{
// Calculating sum of value1 and value2
let sum = variable1 + variable2
// Print the sum
print("The sum of", variable1, "and",
variable2, "is equal to", sum)
}
// Function 3 to calculate sum
func printSum(number1: Int, number2: Int)
{
// Calculating sum of value1 and value2
let sum = number1 + number2
// Print the sum
print("The sum of", number1, "and",
number2, "is equal to", sum)
}
// Driver code
// Calling printSum() function to calculate sum
printSum(value1: 15, value2: 15)
// Calling printSum() function to calculate sum
printSum(value1: 30, value2: 30)
// Calling printSum() function to calculate sum
printSum(value1: 60, value2: 60)
Swift
// Swift program to illustrate function overloading
// Function 1 to calculate sum
func printSum(value1: Int, value2: Int) -> Int
{
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
return(sum)
}
// Function 2 to calculate sum
func printSum(value1: Int, value2:Int) -> Double
{
// Calculating sum of value1 and value2
let sum = value1 + value1
// Print the sum
return (Double)(sum)
}
// Driver code
// Calling printSum() function to calculate sum
print("The sum of 15 and 25 is equal to",
printSum(value1: 15, value2: 25))
// Calling printSum() function to calculate sum
print("The sum of 25 and 35 is equal to",
printSum(value1: 25, value2: 35))
输出:
GeeksforGeeks
函数重载
函数重载是开发人员最常用的功能。这种技术允许他们创建具有一些共同特征的函数,例如同名等。在 Swift 中,一对函数可以具有相同的名称,只要它们的参数数量不同,至少一个对应的类型不同参数,或至少一个对应参数的标签。这些函数称为重载函数,此功能称为函数重载。它提供了更大的灵活性,并且在我们需要相同或相似的结果或功能时很有用。
我们可以使用三种方法在 Swift 中实现函数重载,下面将讨论它们:
不同数量参数的函数重载
对应参数的数据类型相同,但包含的参数个数不同的函数,可以使用相同的函数名。在下面的程序中,我们有三个同名的函数 printSum,但参数数量不同。这里,第一个函数包含两个整数类型的参数,第二个函数包含三个整数类型的参数,而第三个函数包含四个整数类型的参数。
例子:
迅速
// Swift program to illustrate function overloading
// Function 1 to calculate sum
func printSum(value1: Int, value2: Int)
{
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
print("The sum of", value1, "and",
value2, "is equal to", sum)
}
// Function 2 to calculate sum
func printSum(value1: Int, value2: Int, value3: Int)
{
// Calculating sum of value1 and value2
let sum = value1 + value2 + value3
// Print the sum
print("The sum of", value1, ",",
value2, "and", value3,
"is equal to", sum)
}
// Function 3 to calculate sum
func printSum(value1: Int, value2: Int, value3: Int,
value4: Int)
{
// Calculating sum of value1 and value2
let sum = value1 + value2 + value3 + value4
// Print the sum
print("The sum of", value1, ",",
value2, ",", value3, "and",
value4, "is equal to", sum)
}
// Calling printSum() function to calculate sum
printSum(value1: 5, value2: 15)
// Calling printSum() function to calculate sum
printSum(value1: 5, value2: 15, value3: 25)
// Calling printSum() function to calculate sum
printSum(value1: 5, value2: 15, value3: 25, value4: 35)
输出:
The sum of 5 and 15 is equal to 20
The sum of 5 , 15 and 25 is equal to 45
The sum of 5 , 15 , 25 and 35 is equal to 80
相同数量参数的函数重载
参数个数相同但对应的参数至少有一个数据类型不同的函数可以同名。在下面的程序中,我们有三个同名的函数 printSum,虽然所有函数都包含相同数量的参数但类型不同。第一个函数包含两个整数类型参数,第二个函数包含一个整数和一个浮点类型参数,第三个函数包含两个浮点类型参数。
例子:
迅速
// Swift program to illustrate function overloading
// Function 1 to calculate sum
func printSum(value1: Int, value2: Int)
{
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
print("The sum of", value1, "and",
value2, "is equal to", sum)
}
// Function 2 to calculate sum
func printSum(value1: Int, value2: Float)
{
// Calculating sum of value1 and value2
let sum = Float(value1) + value2
// Print the sum
print("The sum of", value1, "and",
value2, "is equal to", sum)
}
// Function 3 to calculate sum
func printSum(value1: Float, value2: Float)
{
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
print("The sum of", value1, "and",
value2, "is equal to", sum)
}
// Calling printSum() function to calculate sum
printSum(value1: 5, value2: 15)
// Calling printSum() function to calculate sum
printSum(value1: 5, value2: 15.123)
// Calling printSum() function to calculate sum
printSum(value1: 5.123, value2: 15.123)
输出:
The sum of 5 and 15 is equal to 20
The sum of 5 and 15.123 is equal to 20.123001
The sum of 5.123 and 15.123 is equal to 20.246
具有不同参数标签的函数重载
对应参数的数据类型相同、参数个数相同但标号不同的两个函数可以使用相同的函数名。在下面的程序中,我们有三个同名的函数 printSum,虽然所有函数都包含相同数量和类型的参数,但标签不同。第一个函数包含两个带有 value1 和 value2 标签的整数类型参数,第二个函数包含两个带有 variable1 和 variable2 标签的整数类型参数,第三个函数包含两个带有 number1 和 number2 标签的整数类型参数。
例子:
迅速
// Swift program to illustrate function overloading
// Function 1 to calculate sum
func printSum(value1: Int, value2: Int)
{
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
print("The sum of", value1, "and",
value2, "is equal to", sum)
}
// Function 2 to calculate sum
func printSum(variable1: Int, variable2:Int)
{
// Calculating sum of value1 and value2
let sum = variable1 + variable2
// Print the sum
print("The sum of", variable1, "and",
variable2, "is equal to", sum)
}
// Function 3 to calculate sum
func printSum(number1: Int, number2: Int)
{
// Calculating sum of value1 and value2
let sum = number1 + number2
// Print the sum
print("The sum of", number1, "and",
number2, "is equal to", sum)
}
// Driver code
// Calling printSum() function to calculate sum
printSum(value1: 15, value2: 15)
// Calling printSum() function to calculate sum
printSum(value1: 30, value2: 30)
// Calling printSum() function to calculate sum
printSum(value1: 60, value2: 60)
输出:
The sum of 15 and 15 is equal to 30
The sum of 30 and 30 is equal to 60
The sum of 60 and 60 is equal to 120
根据返回类型重载函数
我们可以仅根据函数的返回类型重载函数吗?答案是否定的。我们不能通过对函数使用不同的返回类型来重载函数。换句话说,如果程序中至少有两个函数具有相同数据类型的对应参数并且具有完全相同数量的参数,则编译器会产生编译时错误。在下面的程序中,第一个函数具有整数返回类型并包含两个整数类型参数,第二个函数具有双精度返回类型并包含两个整数类型参数。此外,相应的参数具有相同的标签。因此,这在 Swift 中是不允许的。
例子:
迅速
// Swift program to illustrate function overloading
// Function 1 to calculate sum
func printSum(value1: Int, value2: Int) -> Int
{
// Calculating sum of value1 and value2
let sum = value1 + value2
// Print the sum
return(sum)
}
// Function 2 to calculate sum
func printSum(value1: Int, value2:Int) -> Double
{
// Calculating sum of value1 and value2
let sum = value1 + value1
// Print the sum
return (Double)(sum)
}
// Driver code
// Calling printSum() function to calculate sum
print("The sum of 15 and 25 is equal to",
printSum(value1: 15, value2: 25))
// Calling printSum() function to calculate sum
print("The sum of 25 and 35 is equal to",
printSum(value1: 25, value2: 35))
输出:
main.swift:28:7: error: ambiguous use of 'printSum(value1:value2:)'
printSum(value1: 15, value2: 25))
^
main.swift:4:6: note: found this candidate
func printSum(value1: Int, value2: Int) -> Int
^
main.swift:15:6: note: found this candidate
func printSum(value1: Int, value2:Int) -> Double
^
main.swift:32:7: error: ambiguous use of 'printSum(value1:value2:)'
printSum(value1: 25, value2: 35))
^
main.swift:4:6: note: found this candidate
func printSum(value1: Int, value2: Int) -> Int
^
main.swift:15:6: note: found this candidate
func printSum(value1: Int, value2:Int) -> Double
^