Recursion :
Recursion means defining a function in terms of itself; in other words, the function calls itself.
The following example shows recursion in action.
This example will returns the sqaure of even numbers upto the upperLimit starting from the startNumber ascending order, and from there it returns the numbers in decending order.
let us see the example:
publicstatic int methodCall(int startNumber,int upperLimit)
{
if (startNumber < upperLimit)
{
Console.Write(" " + startNumber);
methodCall(startNumber * 2,upperLimit);
}
Console.Write(" " + startNumber);
return startNumber;
}
Call this Method in the main method as
methodCall(2,32);
OUTPUT:
2 4 8 16 32 16 8 4 2
Conclusion:
With out using any loops we can achieve the desired result with recursion, but here the problem is function calling.
Recursion means defining a function in terms of itself; in other words, the function calls itself.
The following example shows recursion in action.
This example will returns the sqaure of even numbers upto the upperLimit starting from the startNumber ascending order, and from there it returns the numbers in decending order.
let us see the example:
publicstatic int methodCall(int startNumber,int upperLimit)
{
if (startNumber < upperLimit)
{
Console.Write(" " + startNumber);
methodCall(startNumber * 2,upperLimit);
}
Console.Write(" " + startNumber);
return startNumber;
}
Call this Method in the main method as
methodCall(2,32);
OUTPUT:
2 4 8 16 32 16 8 4 2
Conclusion:
With out using any loops we can achieve the desired result with recursion, but here the problem is function calling.