+ 4
What is recursion ? ?
can u explain me ? in ruby ?
5 Answers
+ 14
https://www.leighhalliday.com/recursion-in-ruby
+ 5
A function that returns itself and has a base case.
for example,
let the base case, f(0) be 1
Then, you want to have f(1)=1, f(2)=2, f(3)=6, f+4)=24, you return n*f(n-1)
A function sample:
def fun(x)
if x==0
return 1
else
return fun(x-1)*x
end
end
+ 2
Recursion, basically, means applying a code on a big thing, finding a smaller version of the big thing in the thing itself, then applying the same script to each smaller thing in the big thing.
My best example is folder operations: If you recurse, you do the same operation on each folder within the folder itself.
+ 1
if x == 1 then? ???