- #1
- 23,493
- 5,773
I've been studying C# programming, and I came across the following method for calculating factorials that I find confusing. It looks like, in the body associated with "else," the method calls on itself. As a guy with a lot of Fortran coding experience, I've never seen anything like this. I ran the program that contains this method, and it executed fine.
But I find all this very strange. Do C# methods really allow for internal calls to themselves and, if so, what is the logical sequence of operations and values that ensues?
Code:
long factorial (int dataValue)
{
if (dataValue == 1)
{
return 1;
}
else
{
return dataValue * factorial(dataValue - 1);
}
}
But I find all this very strange. Do C# methods really allow for internal calls to themselves and, if so, what is the logical sequence of operations and values that ensues?
Last edited by a moderator: