+ 1

What does the question mark do in this function, and why is it necessary?

func factorial(n: Int) -> Int { return n == 0 ? 1 : n * factorial(n: n-1) }

27th Jul 2018, 2:49 AM
AJ Harris
AJ Harris - avatar
3 odpowiedzi
+ 3
It is a ternary operator. It works like an if statement. - https://syntaxdb.com/ref/swift/ternary
27th Jul 2018, 3:58 AM
Mickel
Mickel - avatar
0
It is an ternary operator similar to if else clause. Hope this helps☺️☺️.
27th Jul 2018, 3:58 AM
Meet Mehta
Meet Mehta - avatar
0
In the Factorial formula, all the positive integers must be multiplied between the number that appears in the formula and the number 1) Example = 7! = 1 * 2 * 3 * 4 * 5 * 6 * 7 = 5040
25th Oct 2020, 11:29 PM
ALEJANDRA SANTOS AVILA
ALEJANDRA SANTOS AVILA - avatar