0
What is Recursion?
Can someone please explain me what recursion is in ruby? I have completed the course but still can’t understand what recursion is.
4 odpowiedzi
+ 4
Recursion- When a Function calls itself, that is known as recursion.
For a recursive algorithm to terminate you always need a base case and you should make sure that you get closer to that base case in each recursive call, ensuring that you will achieve base case.
example:
def sum_array
if the array is empty
return 0
else head of array + sum_array(tail of array)
end
end
(factorials, Fibonacci sequence are other simple examples to use recursion)
+ 4
recursion is when a function calls itself repeatedly.
function hey(){
console.log("a");
hey()}
b is consoled forever because when the function is called,it calls itself
+ 2
Thank you Rame and Brains. I think I kinda get it now. And GAWEN STEASY, why did you link me to the ruby tutorial? I completed it already and I didn’t understand what the tutorial said about recursion so I asked here.