C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: Recursive algorithms are often used for complex searching and simulation. They add needless complication in other programs.
And: It calls itself again based on an incremented value of the parameter it receives. The program also has a commented-out exception.
Tip: This demonstrates the appearance of the method call stack frames in a recursive algorithm.
End: The primitive example here continues until it sums up a value of 10 by incrementing an integer.
Call stack: The call stack has six method stack frames with the Recursive method signature in them.
C# program that uses recursive method
using System;
class Program
{
static int Recursive(int value, ref int count)
{
count++;
if (value >= 10)
{
// throw new Exception("End");
return value;
}
return Recursive(value + 1, ref count);
}
static void Main()
{
//
// Call recursive method with two parameters.
//
int count = 0;
int total = Recursive(5, ref count);
//
// Write the result from the method calls and also the call count.
//
Console.WriteLine(total);
Console.WriteLine(count);
}
}
Output
10 Total value of 10 was added up.
6 Six method calls.
Exception thrown when throw uncommented:
Unhandled Exception: System.Exception: End
at Program.Recursive(Int32 value, Int32& count) ...Program.cs:line 10
at Program.Recursive(Int32 value, Int32& count) ...Program.cs:line 13
at Program.Recursive(Int32 value, Int32& count) ...Program.cs:line 13
at Program.Recursive(Int32 value, Int32& count) ...Program.cs:line 13
at Program.Recursive(Int32 value, Int32& count) ...Program.cs:line 13
at Program.Recursive(Int32 value, Int32& count) ...Program.cs:line 13
at Program.Main() in ...Program.cs:line 22
Tip: The exception here is a good thing, as it stops a process that would otherwise continue infinitely, wasting time and resources.
C# program that causes StackOverflowException
class Program
{
static void Main()
{
// Do not run this program.
Main();
}
}
Output
Process is terminated due to StackOverflowException.
Result: The compiler does not optimize the tail calls in this program. So it causes a stack overflow.
Thus: The programmer will need to write tail-recursive methods with iteration to achieve optimal performance in C# programs.
C# program that tests for tail recursion
class Program
{
static void Recurse(int remaining)
{
// This method could be optimized with tail recursion.
if (remaining <= 0)
{
return;
}
Recurse(remaining - 1);
}
static void Main()
{
// Attempt to call this method.
Recurse(1000000);
}
}
Output
Process is terminated due to StackOverflowException.
And: In this way, the recursive methods continue until the result is attained (or the algorithm fails).
Tip: You may want to use a count parameter to make sure you don't enter an infinite recursion series.
Also: These algorithms can be represented using an iterative algorithm that is recursive only in a logical sense.
And: This is a good reason to prefer a Stack-based collection over a true recursive method.
StackTip: Recursion can be used to implement certain forms of artificial intelligence.