In Playgroundm, type the following code to find the maximum score and the minimum score in a score array
Func Maxminscore (scores:array<int>)-(maxscore:int,minscore:int)? {//A tuple that can be empty
If scores.isempty{
return nil//To determine if there is a value, no value returns a null value
}
var maxscore = scores[0], Minscore = scores[0]
For score in scores[1..<scores.count]{
Maxscore = Max (Maxscore, score)
Minscore = min (minscore, score)
}
Return (Maxscore:maxscore, Minscore:minscore)//returns results
}
var scores:[int]? = [60,80,90,100,30,26,87]
var allscores = scores?? []
If let result = Maxminscore (allscores) {
println ("The maximum score is \ (Result.maxscore)")
println ("The minimum score is \ (Result.minscore)")
}
Use tuple. Subscript Access Element
If let result = Maxminscore (allscores) {
println ("The maximum score is \ (result.0)")
println ("The minimum score is \ (Result.1)")
}
Assigning a value to a variable after the returned value can be used directly
If let (A, b) = Maxminscore (allscores) {
println ("The maximum score is \ (a)")
println ("The minimum score is \ (b)")
}
Function return value in Swift is tuple