Recursive functions are functions that call themselves. In order for them to work properly, you need to define a base case. The function should get closer to that base case with each recursive call. Most recursive procedures can be replaced with loops, but it’s good to understand how to recognize and implement them. Even if a loop can be used to solve a given problem, recursive solutions tend to be more elegant and sometimes faster.
Recursion:
def countup(n)
return if n == 0
countup(n - 1)
puts n
end
Loop:
def countup(n)
counter = 1
while counter <= n
puts counter
counter += 1
end
end