C
C#3mo ago
Faker

✅ How call stacks work in recursive calls

Hello guys, I've implemented a small funtion to find any nth term in the fibonnaci sequence.
C#
// Find nth term in fibonacci sequence - 0.1.1,...
Console.WriteLine(FibonacciFinder(5));
static int FibonacciFinder(int num)
{
if (num is 1 or 2 or 3) return 1;
else
{
return FibonacciFinder(num - 1) + FibonacciFinder(num - 2);
}
}
C#
// Find nth term in fibonacci sequence - 0.1.1,...
Console.WriteLine(FibonacciFinder(5));
static int FibonacciFinder(int num)
{
if (num is 1 or 2 or 3) return 1;
else
{
return FibonacciFinder(num - 1) + FibonacciFinder(num - 2);
}
}
I don't understand how the recursive calls work under the hood though. Like, I know that num-1 is called first, then when all its call stack is kind of "over" we move to the right hand side but this is still vague. I was wondering if there was kind of a visualization tool to see what's hapenning and what's being return for each call.
6 Replies
Sehra
Sehra3mo ago
visual studio and step through the code. look in locals and call stack window. locals will also show when functions return values
No description
Faker
FakerOP3mo ago
oh ok, we can use the debugger to view the stack call?
Sehra
Sehra3mo ago
yes
Faker
FakerOP3mo ago
I will have a look, thanks !!
canton7
canton73mo ago
$close
MODiX
MODiX3mo ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?