Skip to content

Latest commit

 

History

History
22 lines (17 loc) · 737 Bytes

script.md

File metadata and controls

22 lines (17 loc) · 737 Bytes

Recursion

In recursion, a function calls itself to solve a problem. It's essential to define base cases, which are conditions that stop further recursive calls, thereby avoiding infinite loops. For example, in a factorial calculation, the base case could be when the input number is 0 or 1. Each recursive call should bring the function closer to this base case to ensure efficient and safe execution, preventing stack overflow issues.

func factorial(_ n: Int) -> Int {
    if n <= 1 {
        // Base case
        return 1
    } else {
        // Recursive call
        return n * factorial(n - 1)
    }
}

factorial(5)
// Output: 120

Reference

YouTube 👀