Skip to content

Instantly share code, notes, and snippets.

@islandjoe
Created May 31, 2018 15:21
Show Gist options
  • Save islandjoe/5a51513581754fdbedc50640283e9ef4 to your computer and use it in GitHub Desktop.
Save islandjoe/5a51513581754fdbedc50640283e9ef4 to your computer and use it in GitHub Desktop.
Recursion in Swift
func countDown(from: Int) {
  // Stop condition
  if from < 0 {
    return
  }
  
  print(from)
  // Recursion
  countDown(from: from - 1)
}

countDown(from: 10)

func countUp(from: Int, to: Int) {
  // Stop condition
  if from > to {
    return
  }
  
  print(from)
  // Recursion
  countUp(from: from + 1, to: to)
}

countUp(from: 10, to: 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment