P
Panagiotis Atmatzidis
Hello,
I'm following C. Pine's manual for a smooth intro into ruby & =
programming. I'm just curious about this exercise which should teach =
recursion.
Here is the program sample, which as you can see is a simple factorial =
calculator:
def factorial num
if num < 0
return 'You can\'t take the factoria of a negative number'
end
if num <=3D 1
1
else
num * factorial(num-1)
end
end
Although it introduces many interesting concepts to me (i. e. I did not =
knew that after the "if" statement a single _1_ with no quotes could be =
used) I fail to see how the num changes value. The way I read the code =
here:
num * factorial(num-1)
is 30*29 for "puts factorial(30)" not 30*29*28... which is how it works. =
I've tested the code works fine.
The book although, in my opinion is excellent (so far) for starters, =
does not explain - or it does but I did not understand it nevertheless - =
how this piece of code works.
regards
Panagiotis (atmosx) Atmatzidis
email: (e-mail address removed)
URL: http://www.convalesco.org
GnuPG ID: 0xFC4E8BB4=20
gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xFC4E8BB4
I'm following C. Pine's manual for a smooth intro into ruby & =
programming. I'm just curious about this exercise which should teach =
recursion.
Here is the program sample, which as you can see is a simple factorial =
calculator:
def factorial num
if num < 0
return 'You can\'t take the factoria of a negative number'
end
if num <=3D 1
1
else
num * factorial(num-1)
end
end
Although it introduces many interesting concepts to me (i. e. I did not =
knew that after the "if" statement a single _1_ with no quotes could be =
used) I fail to see how the num changes value. The way I read the code =
here:
num * factorial(num-1)
is 30*29 for "puts factorial(30)" not 30*29*28... which is how it works. =
I've tested the code works fine.
The book although, in my opinion is excellent (so far) for starters, =
does not explain - or it does but I did not understand it nevertheless - =
how this piece of code works.
regards
Panagiotis (atmosx) Atmatzidis
email: (e-mail address removed)
URL: http://www.convalesco.org
GnuPG ID: 0xFC4E8BB4=20
gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xFC4E8BB4