Recursion
A function may call itself with a base case.
Example
func fact(_ n: Int) -> Int { n <= 1 ? 1 : n * fact(n - 1) }
func fib(_ n: Int) -> Int { n < 2 ? n : fib(n - 1) + fib(n - 2) }
print(fact(5), fib(10))Exercise
Base case.
if n <= { return 1 }
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.