0
Can someone explain whats going on in this code step by step?
func factorial(n: Int) -> Int { return n == 0 ? 1 : n * factorial(n: n-1) }
1 Réponse
+ 1
You have function with parameter n. In function body you return result of if statement which checks whether n is equal to 0. If true it returns 1. If false it returns result of multiplication n times function with param n-1.
It is recursive function. That means it do itself as long as n is equal to 0.