Swift: Testing if there are parameters in a function called

This entry is part 30 of 41 in the series Swift

If there are no parameters in a function called, return a NIL.

func getSum(values :[Int])->Int? {
    if(values.count == 0){
        return nil
    }
    var sum=0
    for value in values{
        sum+=value
    }
    return sum
}

if let sum = getSum([1,2,3,4,5]){
    print(sum)
    
} else {
    print("No parameters in function called")
}

if let sum = getSum([]){
    print(sum)
    
} else {
    print("No parameters in function called")
    
}

swift

Series Navigation<< Swift: Saying hello with a closure
Swift: A simple struct >>

Leave a comment

Leave a Reply