C
C#7d 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
Sehra7d 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
FakerOP7d ago
oh ok, we can use the debugger to view the stack call?
Sehra
Sehra7d ago
yes
Faker
FakerOP7d ago
I will have a look, thanks !!
canton7
canton77d ago
$close
MODiX
MODiX7d ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?