It is indeed recursive:
static int g( int k)
{
...
return g(k-1)+g(k-2);
}
The call to g inside g makes it direct recursion (indirect recursion is harder to spot, as it involved
foo
calling
bar
which calls
foo
.
Look at the code, and think about what happens when you call it with a specific number, start small:
For 1 we know that because it's a special case:
g(1) => 1
For 2, a special case again:
g(2) => 1
And try 3:
g(3) => return g(2) + g(1) => 1 + 1 => 2
We know g(2) and g(1) so we can use those values.
And 4:
g(4) => g(3) + g(2) => 2 + 1 => 3
And 5:
g(5) => g(4) + g(3) => 3 + 2 => 5
Finally 6:
g(6) => g(5) + g(4) => 5 + 3 => 8
So this code:
Console.WriteLine ( g(4)+g(5)+","+g(6));
Is just
Console.WriteLine ( 3+5+","+8);
will give you
8,8
Make sense?