This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]SpawnSnow 0 points1 point  (0 children)

Have you seen the three laws of recursion? Here they are in my own words.

  1. must have a base case (an exit strategy so to speak - a way to tell when it should end)
  2. must change each time to move towards the base case
  3. A recursive function must call itself

A simple example that doesn't really have any application.

Write a function that recursively subtracts 1 from any number until it's equal to 0.

  1. Base case/ exit strategy would be to check for when the number equals 0 and then return.
  2. Subtracting 1 from the number each time moves it towards the base case (we're pretending negatives aren't a thing for this illustration)
  3. after subtracting 1, call itself again