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) }

26th Jul 2018, 4:57 AM
AJ Harris
AJ Harris - avatar
1 Odpowiedź
+ 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.
26th Jul 2018, 5:09 AM
Lstiti