sweet swift Return function : wish in Java
import UIKit
import Darwin
func average(nums: Int...) -> Double {
var sum = 0
for num in nums {
sum += num
}
return Double(sum) / Double (nums.count)
}
func sum(nums: Int...) -> Double {
var sum = 0
for num in nums {
sum += num
}
return Double(sum)
}
func doMath(mathOp: String)-> (Int...)->Double{
if mathOp == "average" {
return average
}else{
return sum
}
}
var mathFunc = doMath(mathOp: "average")
print(mathFunc(1,3,4,5,6,78,67,5,4,4,9))
Commentaires
Enregistrer un commentaire